|
| 1 | +/** |
| 2 | + * Copyright (C) 2006-2009 Dustin Sallings |
| 3 | + * Copyright (C) 2009-2014 Couchbase, Inc. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING |
| 21 | + * IN THE SOFTWARE. |
| 22 | + */ |
| 23 | +package net.spy.memcached; |
| 24 | + |
| 25 | +import net.spy.memcached.protocol.binary.BinaryOperationFactory; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.net.InetSocketAddress; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collection; |
| 32 | +import java.util.Collections; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.CountDownLatch; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | + |
| 37 | +import static org.junit.Assert.assertTrue; |
| 38 | + |
| 39 | +/** |
| 40 | + * Verifies the functionality of the {@link MemcachedConnection} that the |
| 41 | + * selector gets woken up automatically if idle. |
| 42 | + */ |
| 43 | +public class WokenUpOnIdleTest { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void shouldWakeUpOnIdle() throws Exception { |
| 47 | + CountDownLatch latch = new CountDownLatch(3); |
| 48 | + MemcachedConnection connection = new InstrumentedConnection( |
| 49 | + latch, |
| 50 | + 1024, |
| 51 | + new BinaryConnectionFactory(), |
| 52 | + Arrays.asList(new InetSocketAddress(11210)), |
| 53 | + Collections.<ConnectionObserver>emptyList(), |
| 54 | + FailureMode.Redistribute, |
| 55 | + new BinaryOperationFactory() |
| 56 | + ); |
| 57 | + |
| 58 | + assertTrue(latch.await(5, TimeUnit.SECONDS)); |
| 59 | + } |
| 60 | + |
| 61 | + static class InstrumentedConnection extends MemcachedConnection { |
| 62 | + final CountDownLatch latch; |
| 63 | + InstrumentedConnection(CountDownLatch latch, int bufSize, ConnectionFactory f, |
| 64 | + List<InetSocketAddress> a, Collection<ConnectionObserver> obs, |
| 65 | + FailureMode fm, OperationFactory opfactory) throws IOException { |
| 66 | + super(bufSize, f, a, obs, fm, opfactory); |
| 67 | + this.latch = latch; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void handleWokenUpSelector() { |
| 72 | + latch.countDown(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments