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.InetAddress;
26 import java.net.InetSocketAddress;
27
28 import org.apache.mina.core.service.IoHandlerAdapter;
29 import org.apache.mina.core.session.IdleStatus;
30 import org.apache.mina.core.session.IoSession;
31 import org.apache.mina.util.AvailablePortFinder;
32 import org.junit.Test;
33
34
35
36
37
38
39 public class DatagramSessionIdleTest {
40
41 private boolean readerIdleReceived;
42
43 private boolean writerIdleReceived;
44
45 private boolean bothIdleReceived;
46
47 private Object mutex = new Object();
48
49 private class TestHandler extends IoHandlerAdapter {
50
51 @Override
52 public void sessionIdle(IoSession session, IdleStatus status) 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(
83 new InetSocketAddress(InetAddress.getByName(null), 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 && (System.currentTimeMillis() - startTime) < (READER_IDLE_TIME + 1) * 1000)
95 try {
96 mutex.wait(READER_IDLE_TIME * 1000);
97 } catch (Exception e) {
98 e.printStackTrace();
99 }
100 }
101
102 assertTrue(readerIdleReceived);
103
104 synchronized (mutex) {
105 while (!writerIdleReceived && (System.currentTimeMillis() - startTime) < (WRITER_IDLE_TIME + 1) * 1000)
106 try {
107 mutex.wait((WRITER_IDLE_TIME - READER_IDLE_TIME) * 1000);
108 } catch (Exception e) {
109 e.printStackTrace();
110 }
111 }
112
113 assertTrue(writerIdleReceived);
114
115 synchronized (mutex) {
116 while (!bothIdleReceived && (System.currentTimeMillis() - startTime) < (BOTH_IDLE_TIME + 1) * 1000)
117 try {
118 mutex.wait((BOTH_IDLE_TIME - WRITER_IDLE_TIME) * 1000);
119 } catch (Exception e) {
120 e.printStackTrace();
121 }
122 }
123
124 assertTrue(bothIdleReceived);
125 }
126 }