1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.communication;
23
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.when;
27
28 import java.io.IOException;
29 import java.net.InetAddress;
30 import java.net.Socket;
31
32 import net.grinder.util.StandardTimeAuthority;
33 import net.grinder.util.TimeAuthority;
34
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41
42
43
44
45
46
47
48 public class TestIdleAwareSocketWrapper {
49
50 private static Acceptor s_acceptor;
51 private Socket m_socket;
52 @Mock private TimeAuthority m_timeAuthority;
53
54 @BeforeClass public static void setUpAcceptor() throws Exception {
55 s_acceptor = new Acceptor("localhost", 0, 1, new StandardTimeAuthority());
56 }
57
58 @AfterClass public static void shutDownAcceptor() throws Exception {
59 s_acceptor.shutdown();
60 }
61
62 @Before public void createSocket() throws Exception {
63 MockitoAnnotations.initMocks(this);
64
65 m_socket = new Socket(InetAddress.getByName(null), s_acceptor.getPort());
66 }
67
68 @Test(expected=CommunicationException.class)
69 public void testConstructionWithBadSocket() throws Exception {
70 m_socket.close();
71 new IdleAwareSocketWrapper(m_socket, m_timeAuthority);
72 }
73
74 @Test public void testHasDataNoData() throws Exception {
75 final IdleAwareSocketWrapper socketWrapper =
76 new IdleAwareSocketWrapper(m_socket, m_timeAuthority);
77
78 assertFalse(socketWrapper.hasData(99));
79 }
80
81 @Test(expected = IOException.class)
82 public void testHasDataSocketClosed() throws Exception {
83 final IdleAwareSocketWrapper socketWrapper =
84 new IdleAwareSocketWrapper(m_socket, m_timeAuthority);
85 socketWrapper.close();
86
87 socketWrapper.hasData(99);
88 }
89
90 @Test public void testHasDataTimeOut() throws Exception {
91
92 final IdleAwareSocketWrapper socketWrapper =
93 new IdleAwareSocketWrapper(m_socket,
94 m_timeAuthority);
95
96 when(m_timeAuthority.getTimeInMilliseconds())
97 .thenReturn(1000L)
98 .thenReturn(2000L);
99
100 assertFalse(socketWrapper.hasData(123));
101 assertFalse(m_socket.isClosed());
102
103 assertFalse(socketWrapper.hasData(123));
104 assertTrue(m_socket.isClosed());
105 }
106 }