1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.grinder.engine.agent;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Matchers.contains;
30 import static org.mockito.Mockito.doThrow;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.verifyNoMoreInteractions;
33
34 import java.io.File;
35
36 import net.grinder.common.GrinderException;
37 import net.grinder.engine.agent.TestAgentDaemon.ActionListSleeperStubFactory.SleepAction;
38 import net.grinder.testutility.AbstractJUnit4FileTestCase;
39 import net.grinder.util.Sleeper;
40 import net.grinder.util.Sleeper.ShutdownException;
41
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.slf4j.Logger;
47
48
49
50
51
52
53
54
55
56 public class TestAgentDaemon extends AbstractJUnit4FileTestCase {
57
58 @Mock private Logger m_logger;
59 @Mock private Agent m_agent;
60
61 @Before public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
63 }
64
65 @Test public void testConstruction() throws Exception {
66 final File propertyFile = new File(getDirectory(), "properties");
67 final Agent agent = new AgentImplementation(m_logger, propertyFile, false);
68 final AgentDaemon daemon = new AgentDaemon(m_logger, 1000, agent);
69 daemon.shutdown();
70
71 verify(m_logger).info(contains("finished"));
72 verifyNoMoreInteractions(m_logger);
73 }
74
75 @Test public void testRun() throws Exception {
76 final ActionListSleeperStubFactory sleeperStubFactory =
77 new ActionListSleeperStubFactory(
78 new SleepAction[] {
79 new SleepAction() {
80 public void sleep(long time) throws ShutdownException {
81 assertEquals(1000, time);
82 }
83 },
84 new SleepAction() {
85 public void sleep(long time) throws ShutdownException {
86 assertEquals(1000, time);
87 throw new ShutdownException("");
88 }
89 }
90 } );
91
92 final AgentDaemon agentDaemon =
93 new AgentDaemon(m_logger, 1000, m_agent, sleeperStubFactory.getStub());
94
95 agentDaemon.run();
96
97 sleeperStubFactory.assertFinished();
98 }
99
100 @Test public void testShutdownHook() throws Exception {
101 final AgentDaemon agentDaemon = new AgentDaemon(m_logger, 0, m_agent);
102
103 final Thread shutdownHook = agentDaemon.getShutdownHook();
104
105 assertFalse(Runtime.getRuntime().removeShutdownHook(shutdownHook));
106 verifyNoMoreInteractions(m_agent);
107
108 final GrinderException runException = new GrinderException("") {};
109 doThrow(runException).when(m_agent).run();
110
111 try {
112 agentDaemon.run();
113 fail("Expected GrinderException");
114 }
115 catch (GrinderException e) {
116 assertSame(runException, e);
117 }
118 assertTrue(Runtime.getRuntime().removeShutdownHook(shutdownHook));
119
120 shutdownHook.run();
121 verifyNoMoreInteractions(m_logger);
122 verify(m_agent).shutdown();
123 }
124
125 public static class ActionListSleeperStubFactory
126
127
128 extends net.grinder.testutility.RandomStubFactory<
129 net.grinder.util.Sleeper> {
130
131 public interface SleepAction {
132 void sleep(long time) throws ShutdownException;
133 }
134
135 private final SleepAction[] m_actions;
136 private int m_nextRunnable = 0;
137
138 public ActionListSleeperStubFactory(SleepAction[] actions) {
139 super(Sleeper.class);
140 m_actions = actions;
141 }
142
143 public void override_sleepNormal(Object proxy, long time)
144 throws ShutdownException {
145 m_actions[m_nextRunnable++].sleep(time);
146 }
147
148 public void assertFinished() {
149 assertTrue("All actions complete", m_nextRunnable == m_actions.length);
150 }
151 }
152 }