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) throws Exception {
52 if (status == IdleStatus.BOTH_IDLE) {
53 bothIdleReceived = true;
54 } else if (status == IdleStatus.READER_IDLE) {
55 readerIdleReceived = true;
56 } else if (status == IdleStatus.WRITER_IDLE) {
57 writerIdleReceived = true;
58 }
59
60 synchronized (mutex) {
61 mutex.notifyAll();
62 }
63
64 super.sessionIdle(session, status);
65 }
66 }
67
68 @Test
69 public void testSessionIdle() throws Exception {
70 final int READER_IDLE_TIME = 3;
71 final int WRITER_IDLE_TIME = READER_IDLE_TIME + 2;
72 final int BOTH_IDLE_TIME = WRITER_IDLE_TIME + 2;
73
74 NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
75 acceptor.getSessionConfig().setBothIdleTime(BOTH_IDLE_TIME);
76 acceptor.getSessionConfig().setReaderIdleTime(READER_IDLE_TIME);
77 acceptor.getSessionConfig().setWriterIdleTime(WRITER_IDLE_TIME);
78 InetSocketAddress bindAddress = new InetSocketAddress(AvailablePortFinder.getNextAvailable());
79 acceptor.setHandler(new TestHandler());
80 acceptor.bind(bindAddress);
81 IoSession session = acceptor.newSession(
82 new InetSocketAddress("127.0.0.1", AvailablePortFinder.getNextAvailable()), bindAddress);
83
84
85 assertEquals(BOTH_IDLE_TIME, session.getConfig().getBothIdleTime());
86 assertEquals(READER_IDLE_TIME, session.getConfig().getReaderIdleTime());
87 assertEquals(WRITER_IDLE_TIME, session.getConfig().getWriterIdleTime());
88
89
90 long startTime = System.currentTimeMillis();
91
92 synchronized (mutex) {
93 while (!readerIdleReceived && (System.currentTimeMillis() - startTime) < (READER_IDLE_TIME + 1) * 1000)
94 try {
95 mutex.wait(READER_IDLE_TIME * 1000);
96 } catch (Exception e) {
97 e.printStackTrace();
98 }
99 }
100
101 assertTrue(readerIdleReceived);
102
103 synchronized (mutex) {
104 while (!writerIdleReceived && (System.currentTimeMillis() - startTime) < (WRITER_IDLE_TIME + 1) * 1000)
105 try {
106 mutex.wait((WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000);
107 } catch (Exception e) {
108 e.printStackTrace();
109 }
110 }
111
112 assertTrue(writerIdleReceived);
113
114 synchronized (mutex) {
115 while (!bothIdleReceived && (System.currentTimeMillis() - startTime) < (BOTH_IDLE_TIME + 1) * 1000)
116 try {
117 mutex.wait((BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000);
118 } catch (Exception e) {
119 e.printStackTrace();
120 }
121 }
122
123 assertTrue(bothIdleReceived);
124 }
125 }