Last active
March 4, 2017 22:15
-
-
Save brianm/516eb382f39cbfd567e08500cbcd1347 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.skife.memcake; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.ClassRule; | |
import org.junit.Test; | |
import org.skife.memcake.connection.Connection; | |
import org.skife.memcake.connection.Value; | |
import org.skife.memcake.connection.Version; | |
import java.nio.channels.AsynchronousSocketChannel; | |
import java.nio.charset.StandardCharsets; | |
import java.time.Duration; | |
import java.util.Optional; | |
import java.util.concurrent.CompletableFuture; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.ScheduledExecutorService; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class MemcakeTest { | |
@ClassRule | |
public static final MemcachedRule memcached = new MemcachedRule(); | |
private Connection c; | |
private static ScheduledExecutorService cron = Executors.newScheduledThreadPool(1); | |
@Before | |
public void setUp() throws Exception { | |
c = Connection.open(memcached.getAddress(), | |
AsynchronousSocketChannel.open(), | |
cron, | |
Duration.ofHours(1)).get(); | |
// yes yes, we use the thing under test to clean up after itself. It works though. | |
c.flush(0).get(); | |
} | |
@After | |
public void tearDown() throws Exception { | |
c.close(); | |
} | |
@Test | |
public void testSetWithStrings() throws Exception { | |
Memcake mc = Memcake.create(memcached.getAddress()); | |
CompletableFuture<Version> fs = mc.set("hello", "world") | |
.expires(0) | |
.flags(1) | |
.timeout(Duration.ofHours(1)) | |
.version(Version.NONE) | |
.execute(); | |
Version ver = fs.get(); | |
Optional<Value> val = c.get("hello".getBytes(StandardCharsets.UTF_8)).get(); | |
assertThat(val).isPresent(); | |
val.ifPresent((v) -> { | |
assertThat(v.getValue()).isEqualTo("world".getBytes(StandardCharsets.UTF_8)); | |
assertThat(v.getFlags()).isEqualTo(1); | |
assertThat(v.getVersion()).isEqualTo(ver); | |
}); | |
} | |
@Test | |
public void testGetWithStrings() throws Exception { | |
Memcake mc = Memcake.create(memcached.getAddress()); | |
CompletableFuture<Version> fs = mc.set("hello", "world") | |
.expires(0) | |
.flags(1) | |
.timeout(Duration.ofHours(1)) | |
.version(Version.NONE) | |
.execute(); | |
Version ver = fs.get(); | |
CompletableFuture<Optional<Value>> rs = mc.get("hello".getBytes(StandardCharsets.UTF_8)) | |
.timeout(Duration.ofHours(1)) | |
.execute(); | |
Optional<Value> ov = rs.get(); | |
assertThat(ov).isPresent(); | |
ov.ifPresent((v) -> { | |
assertThat(v.getVersion()).isEqualTo(ver); | |
assertThat(v.getFlags()).isEqualTo(1); | |
assertThat(v.getValue()).isEqualTo("world".getBytes(StandardCharsets.UTF_8)); | |
}); | |
} | |
@Test | |
public void testGetWithMap() throws Exception { | |
Memcake mc = Memcake.create(memcached.getAddress()); | |
mc.set("hello", "world").execute().get(); | |
CompletableFuture<Optional<String>> rs = mc.get("hello") | |
.execute() | |
.thenApply(ov -> ov.map(v -> new String(v.getValue(), | |
StandardCharsets.UTF_8))); | |
Optional<String> ov = rs.get(); | |
assertThat(ov).contains("world"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment