Browse code

Cleanup godoc / tests for router, event queue

Paul Morie authored on 2014/11/27 17:18:27
Showing 6 changed files
... ...
@@ -60,7 +60,7 @@ const (
60 60
 // The watch event effect matrix defines the valid event sequences and what their effects are on
61 61
 // the state of the event queue.
62 62
 //
63
-// A watch event that produces an invalid sequence is dropped (but this should not happen).
63
+// A watch event that produces an invalid sequence results in a panic.
64 64
 var watchEventEffectMatrix = map[watch.EventType]map[watch.EventType]watchEventEffect{
65 65
 	watch.Added: {
66 66
 		watch.Modified: watchEventEffectCompress,
... ...
@@ -86,7 +86,7 @@ var watchEventCompressionMatrix = map[watch.EventType]map[watch.EventType]watch.
86 86
 }
87 87
 
88 88
 // handleEvent is called by Add, Update, and Delete to determine the effect
89
-// of an event of the queue, realize that effect, and update the underlying store
89
+// of an event of the queue, realize that effect, and update the underlying store.
90 90
 func (eq *EventQueue) handleEvent(id string, obj interface{}, newEventType watch.EventType) {
91 91
 	eq.lock.Lock()
92 92
 	defer eq.lock.Unlock()
... ...
@@ -127,8 +127,8 @@ func (eq *EventQueue) handleEvent(id string, obj interface{}, newEventType watch
127 127
 	}
128 128
 }
129 129
 
130
-// updateStore updates the stored value for the given id.  Notice that deletions are not handled
131
-// here - they are performed in Pop in order to provide the deleted value on watch.Deleted events.
130
+// updateStore updates the stored value for the given id.  Note that deletions are not handled
131
+// here; they are performed in Pop in order to provide the deleted value on watch.Deleted events.
132 132
 func (eq *EventQueue) updateStore(id string, obj interface{}, eventType watch.EventType) {
133 133
 	if eventType == watch.Deleted {
134 134
 		return
... ...
@@ -25,54 +25,38 @@ import (
25 25
 func TestEventQueue_basic(t *testing.T) {
26 26
 	q := NewEventQueue()
27 27
 
28
-	q.Add("boo", 2)
29
-	q.Add("foo", 10)
30
-	q.Add("bar", 1)
31
-	q.Update("foo", 11)
32
-	q.Update("foo", 13)
33
-	q.Delete("bar")
34
-	q.Add("zab", 30)
35
-
36
-	event, thing := q.Pop()
37
-
38
-	if thing != 2 {
39
-		t.Fatalf("expected %v, got %v", 2, thing)
40
-	}
41
-
42
-	if event != watch.Added {
43
-		t.Fatalf("expected %s, got %s", watch.Added, event)
44
-	}
45
-
46
-	q.Update("boo", 3)
47
-
48
-	event, thing = q.Pop()
49
-
50
-	if thing != 13 {
51
-		t.Fatalf("expected %v, got %v", 13, thing)
52
-	}
53
-
54
-	if event != watch.Added {
55
-		t.Fatalf("expected %s, got %s", watch.Added, event)
56
-	}
57
-
58
-	event, thing = q.Pop()
59
-
60
-	if thing != 30 {
61
-		t.Fatalf("expected %v, got %v", 30, thing)
62
-	}
63
-
64
-	if event != watch.Added {
65
-		t.Fatalf("expected %s, got %s", watch.Added, event)
66
-	}
67
-
68
-	event, thing = q.Pop()
69
-
70
-	if thing != 3 {
71
-		t.Fatalf("expected %v, got %v", 3, thing)
72
-	}
73
-
74
-	if event != watch.Modified {
75
-		t.Fatalf("expected %s, got %s", watch.Modified, event)
28
+	const amount = 500
29
+	go func() {
30
+		for i := 0; i < amount; i++ {
31
+			q.Add(string([]rune{'a', rune(i)}), i+1)
32
+		}
33
+	}()
34
+	go func() {
35
+		for u := uint(0); u < amount; u++ {
36
+			q.Add(string([]rune{'b', rune(u)}), u+1)
37
+		}
38
+	}()
39
+
40
+	lastInt := int(0)
41
+	lastUint := uint(0)
42
+	for i := 0; i < amount*2; i++ {
43
+		_, obj := q.Pop()
44
+
45
+		switch obj.(type) {
46
+		case int:
47
+			if obj.(int) <= lastInt {
48
+				t.Errorf("got %v (int) out of order, last was %v", obj, lastInt)
49
+			}
50
+			lastInt = obj.(int)
51
+		case uint:
52
+			if obj.(uint) <= lastUint {
53
+				t.Errorf("got %v (uint) out of order, last was %v", obj, lastUint)
54
+			} else {
55
+				lastUint = obj.(uint)
56
+			}
57
+		default:
58
+			t.Fatalf("unexpected type %#v", obj)
59
+		}
76 60
 	}
77 61
 }
78 62
 
... ...
@@ -9,10 +9,13 @@ import (
9 9
 	routeapi "github.com/openshift/origin/pkg/route/api"
10 10
 )
11 11
 
12
+// RouteRegistry provides an in-memory implementation of
13
+// the route.Registry interface.
12 14
 type RouteRegistry struct {
13 15
 	Routes *routeapi.RouteList
14 16
 }
15 17
 
18
+// NewRouteRegistry creates a new RouteRegistry.
16 19
 func NewRouteRegistry() *RouteRegistry {
17 20
 	return &RouteRegistry{}
18 21
 }
... ...
@@ -37,9 +37,9 @@ func TestHandleRoute(t *testing.T) {
37 37
 		"deleted":  {eventType: watch.Deleted, existing: true, aliasRemoved: true},
38 38
 	}
39 39
 
40
-	for name, c := range cases {
40
+	for name, tc := range cases {
41 41
 		existingFrontends := map[string]router.Frontend{}
42
-		if c.existing {
42
+		if tc.existing {
43 43
 			existingFrontends[testRouteServiceName] = router.Frontend{}
44 44
 		}
45 45
 
... ...
@@ -50,12 +50,12 @@ func TestHandleRoute(t *testing.T) {
50 50
 				panic("Unreachable")
51 51
 			},
52 52
 			NextRoute: func() (watch.EventType, *routeapi.Route) {
53
-				return c.eventType, &testRoute
53
+				return tc.eventType, &testRoute
54 54
 			},
55 55
 		}
56 56
 
57 57
 		expectedFrontends := 0
58
-		if c.frontendCreated {
58
+		if tc.frontendCreated {
59 59
 			expectedFrontends = 1
60 60
 		}
61 61
 
... ...
@@ -65,7 +65,7 @@ func TestHandleRoute(t *testing.T) {
65 65
 			t.Errorf("Case %v: Frontend should have been created", name)
66 66
 		}
67 67
 
68
-		if c.aliasAdded {
68
+		if tc.aliasAdded {
69 69
 			addedAlias, ok := testRouter.AddedAliases[testRouteHost]
70 70
 			if !ok {
71 71
 				t.Errorf("Case %v: An alias should have been added for %v", name, testRouteHost)
... ...
@@ -76,7 +76,7 @@ func TestHandleRoute(t *testing.T) {
76 76
 			}
77 77
 		}
78 78
 
79
-		if c.aliasRemoved {
79
+		if tc.aliasRemoved {
80 80
 			removedAlias, ok := testRouter.RemovedAliases[testRouteHost]
81 81
 			if !ok {
82 82
 				t.Errorf("Case %v: An alias should have been removed for %v", name, testRouteHost)
... ...
@@ -97,7 +97,7 @@ func TestHandleRoute(t *testing.T) {
97 97
 	}
98 98
 }
99 99
 
100
-func TestHandleEndpointsAdded(t *testing.T) {
100
+func TestHandleEndpoints(t *testing.T) {
101 101
 	var (
102 102
 		testEndpointsName = "testendpoints"
103 103
 		testEndpoints     = kapi.Endpoints{
... ...
@@ -122,9 +122,9 @@ func TestHandleEndpointsAdded(t *testing.T) {
122 122
 		"deleted":  {eventType: watch.Deleted, existing: true},
123 123
 	}
124 124
 
125
-	for name, c := range cases {
125
+	for name, tc := range cases {
126 126
 		existingFrontends := map[string]router.Frontend{}
127
-		if c.existing {
127
+		if tc.existing {
128 128
 			existingFrontends[testEndpointsName] = router.Frontend{}
129 129
 		}
130 130
 
... ...
@@ -132,7 +132,7 @@ func TestHandleEndpointsAdded(t *testing.T) {
132 132
 		controller := RouterController{
133 133
 			Router: testRouter,
134 134
 			NextEndpoints: func() (watch.EventType, *kapi.Endpoints) {
135
-				return c.eventType, &testEndpoints
135
+				return tc.eventType, &testEndpoints
136 136
 			},
137 137
 			NextRoute: func() (watch.EventType, *routeapi.Route) {
138 138
 				panic("Unreachable")
... ...
@@ -140,7 +140,7 @@ func TestHandleEndpointsAdded(t *testing.T) {
140 140
 		}
141 141
 
142 142
 		expectedFrontends := 0
143
-		if c.frontendCreated {
143
+		if tc.frontendCreated {
144 144
 			expectedFrontends = 1
145 145
 		}
146 146
 
... ...
@@ -155,7 +155,7 @@ func TestHandleEndpointsAdded(t *testing.T) {
155 155
 		}
156 156
 
157 157
 		addedRoutes, ok := testRouter.AddedRoutes[testEndpointsName]
158
-		if c.routesAdded {
158
+		if tc.routesAdded {
159 159
 			if !ok {
160 160
 				t.Errorf("Case %v: Two routes should have been added for %v", name, testEndpointsName)
161 161
 			}
... ...
@@ -105,25 +105,28 @@ func (routes *Routes) FindFrontend(name string) (v Frontend, ok bool) {
105 105
 }
106 106
 
107 107
 func (routes *Routes) DeleteBackends(name string) {
108
-	a, ok := routes.GlobalRoutes[name]
108
+	frontend, ok := routes.GlobalRoutes[name]
109 109
 	if !ok {
110 110
 		return
111 111
 	}
112
-	a.Backends = make(map[string]Backend)
113
-	a.EndpointTable = make(map[string]Endpoint)
114
-	routes.GlobalRoutes[name] = a
112
+	frontend.Backends = make(map[string]Backend)
113
+	frontend.EndpointTable = make(map[string]Endpoint)
114
+
115
+	routes.GlobalRoutes[name] = frontend
115 116
 }
116 117
 
117 118
 func (routes *Routes) CreateFrontend(name string, url string) {
118
-	a := Frontend{}
119
-	a.Backends = make(map[string]Backend)
120
-	a.EndpointTable = make(map[string]Endpoint)
121
-	a.Name = name
122
-	a.HostAliases = make([]string, 0)
119
+	frontend := Frontend{
120
+		Name:          name,
121
+		Backends:      make(map[string]Backend),
122
+		EndpointTable: make(map[string]Endpoint),
123
+		HostAliases:   make([]string, 0),
124
+	}
125
+
123 126
 	if url != "" {
124
-		a.HostAliases = append(a.HostAliases, url)
127
+		frontend.HostAliases = append(frontend.HostAliases, url)
125 128
 	}
126
-	routes.GlobalRoutes[a.Name] = a
129
+	routes.GlobalRoutes[frontend.Name] = frontend
127 130
 	routes.WriteRoutes()
128 131
 }
129 132
 
... ...
@@ -133,35 +136,35 @@ func (routes *Routes) DeleteFrontend(name string) {
133 133
 }
134 134
 
135 135
 func (routes *Routes) AddAlias(alias, frontendName string) {
136
-	a := routes.GlobalRoutes[frontendName]
137
-	for _, v := range a.HostAliases {
136
+	frontend := routes.GlobalRoutes[frontendName]
137
+	for _, v := range frontend.HostAliases {
138 138
 		if v == alias {
139 139
 			return
140 140
 		}
141 141
 	}
142 142
 
143
-	a.HostAliases = append(a.HostAliases, alias)
144
-	routes.GlobalRoutes[frontendName] = a
143
+	frontend.HostAliases = append(frontend.HostAliases, alias)
144
+	routes.GlobalRoutes[frontendName] = frontend
145 145
 	routes.WriteRoutes()
146 146
 }
147 147
 
148 148
 func (routes *Routes) RemoveAlias(alias, frontendName string) {
149
-	a := routes.GlobalRoutes[frontendName]
149
+	frontend := routes.GlobalRoutes[frontendName]
150 150
 	newAliases := make([]string, 0)
151
-	for _, v := range a.HostAliases {
151
+	for _, v := range frontend.HostAliases {
152 152
 		if v == alias || v == "" {
153 153
 			continue
154 154
 		}
155 155
 		newAliases = append(newAliases, v)
156 156
 	}
157
-	a.HostAliases = newAliases
158
-	routes.GlobalRoutes[frontendName] = a
157
+	frontend.HostAliases = newAliases
158
+	routes.GlobalRoutes[frontendName] = frontend
159 159
 	routes.WriteRoutes()
160 160
 }
161 161
 
162 162
 func (routes *Routes) AddRoute(frontendName, frontendPath, backendPath string, protocols []string, endpoints []Endpoint) {
163 163
 	var id string
164
-	a := routes.GlobalRoutes[frontendName]
164
+	frontend := routes.GlobalRoutes[frontendName]
165 165
 
166 166
 	epIDs := make([]string, 1)
167 167
 	for newEpId := range endpoints {
... ...
@@ -170,7 +173,7 @@ func (routes *Routes) AddRoute(frontendName, frontendPath, backendPath string, p
170 170
 			continue
171 171
 		}
172 172
 		found := false
173
-		for _, ep := range a.EndpointTable {
173
+		for _, ep := range frontend.EndpointTable {
174 174
 			if ep.IP == newEndpoint.IP && ep.Port == newEndpoint.Port {
175 175
 				epIDs = append(epIDs, ep.ID)
176 176
 				found = true
... ...
@@ -180,27 +183,27 @@ func (routes *Routes) AddRoute(frontendName, frontendPath, backendPath string, p
180 180
 		if !found {
181 181
 			id = makeID()
182 182
 			ep := Endpoint{id, newEndpoint.IP, newEndpoint.Port}
183
-			a.EndpointTable[id] = ep
183
+			frontend.EndpointTable[id] = ep
184 184
 			epIDs = append(epIDs, ep.ID)
185 185
 		}
186 186
 	}
187 187
 	// locate a backend that may already exist with this protocol and fe/be path
188 188
 	found := false
189
-	for _, be := range a.Backends {
189
+	for _, be := range frontend.Backends {
190 190
 		if be.FePath == frontendPath && be.BePath == backendPath && cmpStrSlices(protocols, be.Protocols) {
191 191
 			for _, epId := range epIDs {
192 192
 				be.EndpointIDs = append(be.EndpointIDs, epId)
193 193
 			}
194
-			a.Backends[be.ID] = be
194
+			frontend.Backends[be.ID] = be
195 195
 			found = true
196 196
 			break
197 197
 		}
198 198
 	}
199 199
 	if !found {
200 200
 		id = makeID()
201
-		a.Backends[id] = Backend{id, frontendPath, backendPath, protocols, epIDs, TERM_EDGE, nil}
201
+		frontend.Backends[id] = Backend{id, frontendPath, backendPath, protocols, epIDs, TERM_EDGE, nil}
202 202
 	}
203
-	routes.GlobalRoutes[a.Name] = a
203
+	routes.GlobalRoutes[frontend.Name] = frontend
204 204
 	routes.WriteRoutes()
205 205
 }
206 206
 
... ...
@@ -20,6 +20,7 @@ type Router struct {
20 20
 	RouterReloaded   bool
21 21
 }
22 22
 
23
+// NewRouter creates a new Router.
23 24
 func NewRouter(registeredFrontends map[string]router.Frontend) *Router {
24 25
 	return &Router{
25 26
 		FrontendsToFind:  registeredFrontends,