|
8 | 8 | "os" |
9 | 9 | "reflect" |
10 | 10 | "testing" |
| 11 | + "time" |
11 | 12 |
|
12 | 13 | "github.com/m-lab/traceroute-caller/ipcache" |
13 | 14 |
|
@@ -112,49 +113,51 @@ func TestGetConnectionsWithFakeSS(t *testing.T) { |
112 | 113 | } |
113 | 114 | } |
114 | 115 |
|
115 | | -func TestConnectionPollerConstruction(t *testing.T) { |
116 | | - // The only thing we can verify by default is that the code does not crash. |
117 | | - // Which is not nothing, but it's not a lot. |
118 | | - ctx, cancel := context.WithCancel(context.Background()) |
119 | | - defer cancel() |
120 | | - cache := ipcache.New(ctx) |
121 | | - connPoller := New(cache) |
122 | | - connPoller.GetClosedConnections() |
| 116 | +type testTracer struct { |
| 117 | + calls int |
| 118 | + answers []map[connection.Connection]struct{} |
| 119 | +} |
| 120 | + |
| 121 | +func (tt *testTracer) Trace(conn connection.Connection, t time.Time) string { |
| 122 | + return "Fake Trace test" |
| 123 | +} |
| 124 | + |
| 125 | +func (tt *testTracer) CreateCacheTest(conn connection.Connection, t time.Time, cachedTest string) { |
| 126 | + return |
123 | 127 | } |
124 | 128 |
|
125 | 129 | type testFinder struct { |
126 | | - calls int |
127 | | - answers []map[connection.Connection]struct{} |
128 | 130 | } |
129 | 131 |
|
130 | 132 | func (tf *testFinder) GetConnections() map[connection.Connection]struct{} { |
131 | | - calls := tf.calls |
132 | | - tf.calls++ |
133 | | - return tf.answers[calls] |
| 133 | + conns := make(map[connection.Connection]struct{}) |
| 134 | + return conns |
134 | 135 | } |
135 | 136 |
|
136 | | -func TestGetClosedCollection(t *testing.T) { |
137 | | - // This setup causes both conn3 and conn2 to disappear, but because conn3 is in |
138 | | - // the ipcache, only conn2 should be returned. |
| 137 | +func TestConnectionPollerConstruction(t *testing.T) { |
| 138 | + // The only thing we can verify by default is that the code does not crash. |
| 139 | + // Which is not nothing, but it's not a lot. |
139 | 140 | ctx, cancel := context.WithCancel(context.Background()) |
140 | 141 | defer cancel() |
141 | 142 | cache := ipcache.New(ctx) |
142 | | - connPoller := New(cache).(*connectionPoller) |
143 | | - conn1 := connection.Connection{RemoteIP: "1.1.1.1"} |
144 | | - conn2 := connection.Connection{RemoteIP: "1.1.1.2"} |
145 | | - conn3 := connection.Connection{RemoteIP: "1.1.1.3"} |
146 | | - connPoller.recentIPCache.Add(conn3.RemoteIP) |
147 | | - connPoller.finder = &testFinder{ |
148 | | - answers: []map[connection.Connection]struct{}{ |
149 | | - {conn1: struct{}{}, conn2: struct{}{}, conn3: struct{}{}}, |
150 | | - {conn1: struct{}{}}, |
151 | | - }, |
| 143 | + connPoller := &connectionPoller{ |
| 144 | + finder: &testFinder{}, |
| 145 | + recentIPCache: cache, |
| 146 | + connectionPool: make(map[connection.Connection]struct{}), |
152 | 147 | } |
153 | | - connPoller.connectionPool = connPoller.GetConnections() |
| 148 | + conn1 := connection.Connection{ |
| 149 | + RemoteIP: "1.1.1.2", |
| 150 | + RemotePort: 5034, |
| 151 | + LocalIP: "1.1.1.3", |
| 152 | + LocalPort: 58790, |
| 153 | + Cookie: "10f3d"} |
| 154 | + connPoller.connectionPool[conn1] = struct{}{} |
| 155 | + var tt testTracer |
| 156 | + connPoller.TraceClosedConnections(&tt) |
154 | 157 |
|
155 | | - c := connPoller.GetClosedConnections() |
| 158 | + time.Sleep(200 * time.Millisecond) |
156 | 159 |
|
157 | | - if len(c) != 1 || c[0] != conn2 { |
158 | | - t.Errorf("Wanted %v but got %v", []connection.Connection{conn2}, c) |
| 160 | + if connPoller.recentIPCache.GetCacheLength() != 1 { |
| 161 | + t.Errorf("ConnectionPoller not working properly") |
159 | 162 | } |
160 | 163 | } |
0 commit comments