-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathresulthandler_test.go
134 lines (128 loc) · 3.25 KB
/
resulthandler_test.go
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package llama
import (
"testing"
"time"
)
func TestResultHandlerRun(t *testing.T) {
// Create the TestHandler
in := make(chan *Probe)
out := make(chan *Result)
rh := NewResultHandler(in, out)
// Start it
rh.Run()
// Create a probe
// Use zero values here, but test more intensely further down
probe := &Probe{}
// Provide the probe to the input
select {
case in <- probe:
case <-time.After(time.Second):
t.Error("Was unable to provide the probe to input, blocked channel")
}
// Verify it comes out
select {
case <-out:
case <-time.After(time.Second):
t.Error("Result was not processed/output within 1 second")
}
}
func TestResultHandlerStop(t *testing.T) {
in := make(chan *Probe)
out := make(chan *Result)
rh := NewResultHandler(in, out)
// Make sure it's not already closed
select {
case <-rh.stop:
t.Error("ResultHandle stopped before being told to")
default:
}
// Close it and confirm
rh.Stop()
select {
case <-rh.stop:
default:
t.Error("ResultHandle was told to stop but doesn't appear to be")
}
}
func TestNewResultHandler(t *testing.T) {
in := make(chan *Probe)
out := make(chan *Result)
rh := NewResultHandler(in, out)
if rh.in != in {
t.Error("Provided input channel doesn't match on ResultHandler")
}
if rh.out != out {
t.Error("Provided output channel doesn't match on ResultHandler")
}
}
func TestProcess(t *testing.T) {
pd := &PathDist{}
probe := &Probe{
Pd: pd,
CSent: uint64(100000),
CRcvd: uint64(200000),
Tos: byte(0),
}
// Make sure it processes correctly, but leave specific cases to any tests
// below.
result := Process(probe)
if result.Pd != pd {
t.Error("PathDist doesn't match between Probe and Result")
}
// Make sure the RTT is calculated
if result.RTT != 100000 {
t.Error("RTT was not correctly calculated")
}
// This shouldn't be marked as lost
if result.Lost == true {
t.Error("Result indicates Lost when it shouldn't")
}
// The Done time should make the CRcvd time on the Probe
if result.Done != probe.CRcvd {
t.Error("CRcvd time wasn't propagated to Result")
}
}
func TestRTT(t *testing.T) {
probe := &Probe{
CSent: uint64(100000),
CRcvd: uint64(200000),
}
result := &Result{}
// Calculate positive RTT and verify
err := RTT(probe, result)
if err != nil {
t.Error("Hit error unexpectedly calculating positve RTT")
}
if result.RTT != 100000 {
t.Error("RTT calculation incorrect. Got", result.RTT,
", expected 100000")
}
// Handle a lost probe and verify zero RTT and mark as loss
probe = &Probe{
CSent: uint64(100000),
}
result = &Result{} // Reset the Result
err = RTT(probe, result)
if err != nil {
t.Error("Hit error unexpectedly calculating RTT of lost probe")
}
if result.RTT != 0 || result.Lost != true {
t.Errorf("%+v\n", *probe)
t.Errorf("%+v\n", *result)
t.Error("Lost probe with non-zero RTT or not marked as lost")
}
// Handle a probe that has mixed up sent/recv times
probe = &Probe{
CSent: uint64(200000),
CRcvd: uint64(100000),
}
result = &Result{} // Reset the Result
// Calculate positive RTT and verify
err = RTT(probe, result)
if err == nil {
t.Error("Didn't hit an error when we should have for mixed up times")
}
if result.RTT != 0 || result.Lost != true {
t.Error("Mixed up probe with non-zero RTT or not marked as lost")
}
}