1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package net.sf.jperfprobe;
26
27 import static org.junit.Assert.*;
28 import org.junit.Test;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import java.util.concurrent.CountDownLatch;
33
34
35 public class DefaultProbeTest {
36 final Logger log = LoggerFactory.getLogger(DefaultProbeTest.class);
37
38 @Test
39 public void testRunning() {
40 DefaultProbe probe = new DefaultProbe("PROMP");
41 assertFalse("Probe should have status not running", probe.isRunning());
42 probe.start();
43 assertTrue("Probe should have status running", probe.isRunning());
44 probe.stop();
45 assertFalse("Probe should have status not running", probe.isRunning());
46 probe.disable();
47 probe.start();
48 assertFalse("Probe should have status not running", probe.isRunning());
49 probe.enable();
50 probe.start();
51 probe.disable();
52 assertFalse("Probe should have status not running, when disabled", probe.isRunning());
53 probe.stop();
54 assertFalse("Probe should have status not running, when disabled", probe.isRunning());
55 }
56
57 @Test
58 public void testElapsed() {
59 DefaultProbe probeTest = new DefaultProbe("TEST");
60 try {
61 probeTest.start();
62 Thread.sleep(400);
63 probeTest.stop();
64 } catch (Exception e) {
65 System.out.println("hell is looose");
66 }
67
68 assertTrue("elapsed time is wrong", probeTest.getElapsed() > 390000000 && probeTest.getElapsed() < 450000000);
69
70 MockTime mt = new MockTime();
71 DefaultProbe p = new DefaultProbe("bb", mt);
72 mt.setTime(10);
73 p.start();
74 mt.setTime(24);
75 p.stop();
76 assertEquals("elapsed time is wrong", 14, p.getElapsed());
77 }
78
79 @Test
80 public void testProbeNames() {
81 Probe p = new DefaultProbe("valuba");
82 assertEquals("name should be the same", "valuba", p.getName());
83 }
84
85 @Test
86 public void testEnableDisable() {
87 Probe p1 = new DefaultProbe("p1");
88
89 assertTrue("DefaultProbe p1 should be default enabled", p1.isEnabled());
90 p1.disable();
91 assertFalse("DefaultProbe p1 should be disabled", p1.isEnabled());
92 p1.enable();
93 assertTrue("DefaultProbe p1 should be enabled", p1.isEnabled());
94 }
95
96 @Test
97 public void testStartStopInDifferentThreads() throws Exception {
98 final CountDownLatch doneSignal = new CountDownLatch(2);
99 final MockTime mt = new MockTime();
100 final DefaultProbe p = new DefaultProbe("humplepikk", mt);
101
102 Thread t1 = new Thread() {
103 public void run() {
104 mt.setTime(10);
105 p.start();
106 doneSignal.countDown();
107 System.out.println("balla:" + Thread.currentThread().getName());
108 }
109 };
110
111 Thread t2 = new Thread() {
112 public void run() {
113 mt.setTime(24);
114 p.stop();
115 doneSignal.countDown();
116 System.out.println("hjalla:" + Thread.currentThread().getName());
117 }
118
119 };
120
121 t1.start();
122 t2.start();
123
124 doneSignal.await();
125 assertEquals("elapsed time is wrong", 14, p.getElapsed());
126 }
127
128 @Test
129 public void testGetTime() {
130 DefaultProbe p = new DefaultProbe("OIP");
131 assertTrue("Wrong Time object", p.getTime() instanceof SystemTimeByNanos);
132 MockTime mt = new MockTime();
133 p.setTime(mt);
134 mt.setTime(10);
135 p.start();
136 mt.setTime(24);
137 p.stop();
138 assertEquals("elapsed time is wrong", 14, p.getElapsed());
139 assertTrue("Wrong Time object", p.getTime() instanceof MockTime);
140 }
141
142
143 }