|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.catalina.tribes.transport; |
| 18 | + |
| 19 | +import java.lang.reflect.Proxy; |
| 20 | +import java.util.concurrent.atomic.AtomicReference; |
| 21 | + |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import org.apache.catalina.tribes.ChannelException; |
| 26 | +import org.apache.catalina.tribes.ChannelMessage; |
| 27 | +import org.apache.catalina.tribes.Member; |
| 28 | + |
| 29 | +public class TestPooledSender { |
| 30 | + |
| 31 | + private static final long TIMEOUT = 10000; |
| 32 | + |
| 33 | + |
| 34 | + private static class Pool extends PooledSender { |
| 35 | + @Override |
| 36 | + public DataSender getNewDataSender() { |
| 37 | + return (DataSender) Proxy.newProxyInstance(DataSender.class.getClassLoader(), |
| 38 | + new Class<?>[] { DataSender.class }, (proxy, method, args) -> { |
| 39 | + if (method.getReturnType() == boolean.class) { |
| 40 | + return Boolean.FALSE; |
| 41 | + } |
| 42 | + return null; |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void sendMessage(Member[] destination, ChannelMessage msg) throws ChannelException { |
| 48 | + // NO-OP |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + private static Pool createPool(int size, long maxWait) throws Exception { |
| 54 | + Pool pool = new Pool(); |
| 55 | + pool.setPoolSize(size); |
| 56 | + pool.setMaxWait(maxWait); |
| 57 | + pool.connect(); |
| 58 | + return pool; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + private static Thread getSenderInThread(Pool pool, AtomicReference<DataSender> result, |
| 63 | + AtomicReference<Boolean> interrupted) { |
| 64 | + Thread thread = new Thread(() -> { |
| 65 | + result.set(pool.getSender()); |
| 66 | + interrupted.set(Boolean.valueOf(Thread.currentThread().isInterrupted())); |
| 67 | + }); |
| 68 | + thread.start(); |
| 69 | + return thread; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @Test(timeout = 60000) |
| 74 | + public void testWaitEndsWhenSenderReturned() throws Exception { |
| 75 | + Pool pool = createPool(1, 0); |
| 76 | + DataSender first = pool.getSender(); |
| 77 | + Assert.assertNotNull(first); |
| 78 | + |
| 79 | + AtomicReference<DataSender> result = new AtomicReference<>(); |
| 80 | + Thread waiter = getSenderInThread(pool, result, new AtomicReference<>()); |
| 81 | + Thread.sleep(500); |
| 82 | + Assert.assertTrue(waiter.isAlive()); |
| 83 | + |
| 84 | + pool.returnSender(first); |
| 85 | + waiter.join(TIMEOUT); |
| 86 | + Assert.assertFalse(waiter.isAlive()); |
| 87 | + Assert.assertSame(first, result.get()); |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + @Test(timeout = 60000) |
| 92 | + public void testWaitTimesOut() throws Exception { |
| 93 | + Pool pool = createPool(1, 500); |
| 94 | + Assert.assertNotNull(pool.getSender()); |
| 95 | + |
| 96 | + long start = System.nanoTime(); |
| 97 | + Assert.assertNull(pool.getSender()); |
| 98 | + long elapsed = (System.nanoTime() - start) / 1_000_000; |
| 99 | + Assert.assertTrue("Returned after " + elapsed + "ms", elapsed >= 400); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + @Test(timeout = 60000) |
| 104 | + public void testInterruptEndsWait() throws Exception { |
| 105 | + Pool pool = createPool(1, 0); |
| 106 | + Assert.assertNotNull(pool.getSender()); |
| 107 | + |
| 108 | + AtomicReference<DataSender> result = new AtomicReference<>(); |
| 109 | + AtomicReference<Boolean> interrupted = new AtomicReference<>(); |
| 110 | + Thread waiter = getSenderInThread(pool, result, interrupted); |
| 111 | + Thread.sleep(500); |
| 112 | + waiter.interrupt(); |
| 113 | + waiter.join(TIMEOUT); |
| 114 | + Assert.assertFalse(waiter.isAlive()); |
| 115 | + Assert.assertNull(result.get()); |
| 116 | + Assert.assertEquals(Boolean.TRUE, interrupted.get()); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + @Test(timeout = 60000) |
| 121 | + public void testRaisingLimitEndsWait() throws Exception { |
| 122 | + Pool pool = createPool(1, 0); |
| 123 | + Assert.assertNotNull(pool.getSender()); |
| 124 | + |
| 125 | + AtomicReference<DataSender> result = new AtomicReference<>(); |
| 126 | + Thread waiter = getSenderInThread(pool, result, new AtomicReference<>()); |
| 127 | + Thread.sleep(500); |
| 128 | + Assert.assertTrue(waiter.isAlive()); |
| 129 | + |
| 130 | + pool.setPoolSize(2); |
| 131 | + waiter.join(TIMEOUT); |
| 132 | + Assert.assertFalse(waiter.isAlive()); |
| 133 | + Assert.assertNotNull(result.get()); |
| 134 | + } |
| 135 | +} |
0 commit comments