Search in sources :

Example 1 with OperationFuture

use of net.spy.memcached.internal.OperationFuture in project oxAuth by GluuFederation.

the class MemcacheManual method main.

public static void main(String[] args) throws IOException {
    final MemcachedClient client = createClients();
    final ExecutorService executorService = Executors.newFixedThreadPool(1000, daemonThreadFactory());
    int count = 10000;
    final long start = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        final int key = i;
        executorService.execute(new Runnable() {

            @Override
            public void run() {
                // MemcachedClient client = clients.get(random);
                Object toPut = testGrant();
                // Object toPut = UUID.randomUUID().toString();
                OperationFuture<Boolean> set = null;
                for (int j = 0; j < 3; j++) {
                    set = client.set(Integer.toString(key), 60, toPut);
                }
                // block
                OperationStatus status = set.getStatus();
                System.out.println(" key: " + key + ", time: " + (new Date().getTime() - start) + "ms, set: " + status);
                executorService.execute(new Runnable() {

                    @Override
                    public void run() {
                        int random = random();
                        if (random % 3 == 0) {
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        Object get = client.get(Integer.toString(key));
                        System.out.println("GET key: " + key + " result: " + get);
                    }
                });
            }
        });
    }
    sleep(40);
    // System.out.println(client.get("myKey"));
    //
    // client.set("myKey", 30, testState());
    //
    // sleep(12);
    // System.out.println(client.get("myKey"));
    //
    // sleep(12);
    // System.out.println(client.get("myKey"));
    client.shutdown();
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) OperationStatus(net.spy.memcached.ops.OperationStatus) ExecutorService(java.util.concurrent.ExecutorService) OperationFuture(net.spy.memcached.internal.OperationFuture) Date(java.util.Date)

Example 2 with OperationFuture

use of net.spy.memcached.internal.OperationFuture in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryThenSuccess.

@Test
public void testTimeOutRetryThenSuccess() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(of, times(2)).get();
    verify(msg).setBody(true);
}
Also used : Answer(org.mockito.stubbing.Answer) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Example 3 with OperationFuture

use of net.spy.memcached.internal.OperationFuture in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryTwiceThenSuccess.

@Test
public void testTimeOutRetryTwiceThenSuccess() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    }).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(of, times(3)).get();
    verify(msg).setBody(true);
}
Also used : Answer(org.mockito.stubbing.Answer) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Example 4 with OperationFuture

use of net.spy.memcached.internal.OperationFuture in project camel by apache.

the class CouchbaseProducerTest method testExpiryTimeIsSet.

@Test
public void testExpiryTimeIsSet() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            return true;
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    // Mock out some headers so we can set an expiry
    int expiry = 5000;
    Map<String, Object> testHeaders = new HashMap<String, Object>();
    testHeaders.put("CCB_TTL", Integer.toString(expiry));
    when(msg.getHeaders()).thenReturn(testHeaders);
    when(msg.getHeader(HEADER_TTL, String.class)).thenReturn(Integer.toString(expiry));
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    Message outmsg = mock(Message.class);
    when(exchange.getOut()).thenReturn(msg);
    producer.process(exchange);
    verify(client).set(org.mockito.Matchers.anyString(), Mockito.eq(expiry), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class));
}
Also used : Message(org.apache.camel.Message) HashMap(java.util.HashMap) InvalidPayloadException(org.apache.camel.InvalidPayloadException) ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) Test(org.junit.Test)

Example 5 with OperationFuture

use of net.spy.memcached.internal.OperationFuture in project camel by apache.

the class CouchbaseProducerTest method testTimeOutRetryToException.

@Test
public void testTimeOutRetryToException() throws Exception {
    OperationFuture of = mock(OperationFuture.class);
    when(of.get()).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Exception {
            throw new RuntimeException("Timed out waiting for operation");
        }
    });
    when(client.set(org.mockito.Matchers.anyString(), org.mockito.Matchers.anyInt(), org.mockito.Matchers.anyObject(), org.mockito.Matchers.any(PersistTo.class), org.mockito.Matchers.any(ReplicateTo.class))).thenReturn(of);
    when(endpoint.getId()).thenReturn("123");
    when(endpoint.getOperation()).thenReturn("CCB_PUT");
    try {
        producer.process(exchange);
    } catch (Exception e) {
        // do nothing
        verify(of, times(3)).get();
    }
}
Also used : ReplicateTo(net.spy.memcached.ReplicateTo) PersistTo(net.spy.memcached.PersistTo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OperationFuture(net.spy.memcached.internal.OperationFuture) InvalidPayloadException(org.apache.camel.InvalidPayloadException) Test(org.junit.Test)

Aggregations

OperationFuture (net.spy.memcached.internal.OperationFuture)6 PersistTo (net.spy.memcached.PersistTo)4 ReplicateTo (net.spy.memcached.ReplicateTo)4 InvalidPayloadException (org.apache.camel.InvalidPayloadException)4 Test (org.junit.Test)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 Date (java.util.Date)2 ExecutorService (java.util.concurrent.ExecutorService)2 MemcachedClient (net.spy.memcached.MemcachedClient)2 OperationStatus (net.spy.memcached.ops.OperationStatus)2 Answer (org.mockito.stubbing.Answer)2 HashMap (java.util.HashMap)1 Message (org.apache.camel.Message)1