1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.transport.socket.nio;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.net.InetSocketAddress;
26
27 import org.apache.mina.core.service.IoHandlerAdapter;
28 import org.apache.mina.core.session.IdleStatus;
29 import org.apache.mina.core.session.IoSession;
30 import org.apache.mina.util.AvailablePortFinder;
31 import org.junit.Test;
32
33
34
35
36
37
38 public class DatagramSessionIdleTest {
39
40 private boolean readerIdleReceived;
41
42 private boolean writerIdleReceived;
43
44 private boolean bothIdleReceived;
45
46 private Object mutex = new Object();
47
48 private class TestHandler extends IoHandlerAdapter {
49
50 @Override
51 public void sessionIdle(IoSession session, IdleStatus status)
52 throws Exception {
53 if (status == IdleStatus.BOTH_IDLE) {
54 bothIdleReceived = true;
55 } else if (status == IdleStatus.READER_IDLE) {
56 readerIdleReceived = true;
57 } else if (status == IdleStatus.WRITER_IDLE) {
58 writerIdleReceived = true;
59 }
60
61 synchronized (mutex) {
62 mutex.notifyAll();
63 }
64
65 super.sessionIdle(session, status);
66 }
67 }
68
69 @Test
70 public void testSessionIdle() throws Exception {
71 final int READER_IDLE_TIME = 3;
72 final int WRITER_IDLE_TIME = READER_IDLE_TIME + 2;
73 final int BOTH_IDLE_TIME = WRITER_IDLE_TIME + 2;
74
75 NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
76 acceptor.getSessionConfig().setBothIdleTime(BOTH_IDLE_TIME);
77 acceptor.getSessionConfig().setReaderIdleTime(READER_IDLE_TIME);
78 acceptor.getSessionConfig().setWriterIdleTime(WRITER_IDLE_TIME);
79 InetSocketAddress bindAddress = new InetSocketAddress( AvailablePortFinder.getNextAvailable());
80 acceptor.setHandler(new TestHandler());
81 acceptor.bind(bindAddress);
82 IoSession session = acceptor.newSession(new InetSocketAddress(
83 "127.0.0.1", AvailablePortFinder.getNextAvailable()), bindAddress);
84
85
86 assertEquals(BOTH_IDLE_TIME, session.getConfig().getBothIdleTime());
87 assertEquals(READER_IDLE_TIME, session.getConfig().getReaderIdleTime());
88 assertEquals(WRITER_IDLE_TIME, session.getConfig().getWriterIdleTime());
89
90
91 long startTime = System.currentTimeMillis();
92
93 synchronized (mutex) {
94 while (!readerIdleReceived
95 && (System.currentTimeMillis() - startTime) < (READER_IDLE_TIME + 1) * 1000)
96 try {
97 mutex.wait(READER_IDLE_TIME * 1000);
98 } catch (Exception e) {
99 e.printStackTrace();
100 }
101 }
102
103 assertTrue(readerIdleReceived);
104
105 synchronized (mutex) {
106 while (!writerIdleReceived
107 && (System.currentTimeMillis() - startTime) < (WRITER_IDLE_TIME + 1) * 1000)
108 try {
109 mutex.wait((WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000);
110 } catch (Exception e) {
111 e.printStackTrace();
112 }
113 }
114
115 assertTrue(writerIdleReceived);
116
117 synchronized (mutex) {
118 while (!bothIdleReceived
119 && (System.currentTimeMillis() - startTime) < (BOTH_IDLE_TIME + 1) * 1000)
120 try {
121 mutex.wait((BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000);
122 } catch (Exception e) {
123 e.printStackTrace();
124 }
125 }
126
127 assertTrue(bothIdleReceived);
128 }
129 }