-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrivetest.py
More file actions
179 lines (141 loc) · 4.57 KB
/
drivetest.py
File metadata and controls
179 lines (141 loc) · 4.57 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
##
# simulator.py -
# Module for driving the Trip generator.
##
from __future__ import absolute_import
import sys
import time
import redis
from tripgrid.Trip import *
from tripgrid.TripQuery import *
# redis and celery aren't as synchrounous as needed for testing, so we need a reasonable
# sleep to ensure the data we query has enough time to sync.
DATA_SYNC_SLEEP_TIME_SEC = 5
g_debugLevel = 1
def log(loglevel, *args):
if loglevel <= g_debugLevel:
print args
def initRedis():
r = redis.Redis()
dbsize = r.dbsize()
itr = 0
while dbsize != 0:
r.flushall()
dbsize = r.dbsize()
itr += 1
log(3, "iterations to flush redis: %d", itr)
return r
def startAllTrips(numTrips, trips, testQuadrant=None):
for x in range(0, numTrips):
trip = Trip(0.1, testQuadrant)
trip.start()
trips.append(trip)
def waitForAllTrips(trips):
for trip in trips:
isAlive = True
while isAlive:
trip.join()
isAlive = trip.isAlive()
time.sleep(DATA_SYNC_SLEEP_TIME_SEC)
def printAllTrips(r):
trips = r.smembers("tripIDs")
for t in trips:
locations = r.zrange("tripQueue:%s:locations" % t, 0, -1)
log(3, "tripID %s: %d %r" % (t, len(locations), locations))
def _testQuadrant(bl, tr, expectedTrips, quadStr):
results = {
'trips': [],
'fare': 0,
}
QueryRect(bl, tr, "ALL", "-inf", "inf", False, results)
log(5, "testQuadrant %s reported %d trips ?= %d: %r" \
% (quadStr, len(results['trips']), expectedTrips, results['trips']))
assert(len(results['trips']) == expectedTrips), \
"Error, testQuadrant %s reported %d trips != %d: %r" \
% (quadStr, len(results['trips']), expectedTrips, results['trips'])
log(1, "testQuadrant %s pass" % (quadStr,))
def testAllQuadrants():
r = initRedis()
numTrips = 10
trips = []
startAllTrips(numTrips, trips)
waitForAllTrips(trips)
expectedTrips = numTrips
_testQuadrant((-90, -180), (90, 180), expectedTrips, "all quads")
return True
def testQuadrant(quadrant):
r = initRedis()
numTrips = 10
trips = []
log(1, "Testing Quadrant", quadrant)
startAllTrips(numTrips, trips, quadrant)
waitForAllTrips(trips)
expectedTrips = 0
if quadrant == TOP_LEFT:
expectedTrips = numTrips
if quadrant != LEFT_EDGE and quadrant != TOP_EDGE:
_testQuadrant((-90, 0), (0, 180), expectedTrips, "top left")
expectedTrips = 0
if quadrant == TOP_RIGHT:
expectedTrips = numTrips
if quadrant != RIGHT_EDGE and quadrant != TOP_EDGE:
_testQuadrant((0, 0), (90, 180), expectedTrips, "top right")
expectedTrips = 0
if quadrant == BOTTOM_RIGHT:
expectedTrips = numTrips
if quadrant != RIGHT_EDGE and quadrant != BOTTOM_EDGE:
_testQuadrant((0, -180), (90, 0), expectedTrips, "bottom right")
expectedTrips = 0
if quadrant == BOTTOM_LEFT:
expectedTrips = numTrips
if quadrant != LEFT_EDGE and quadrant != BOTTOM_EDGE:
_testQuadrant((-90, -180), (0, 0), expectedTrips, "bottom left")
expectedTrips = 0
if quadrant == LEFT_EDGE:
expectedTrips = numTrips
_testQuadrant((-90, -180), (-90, 180), expectedTrips, "left edge")
expectedTrips = 0
if quadrant == TOP_EDGE:
expectedTrips = numTrips
_testQuadrant((-90, 180), (90, 180), expectedTrips, "top edge")
expectedTrips = 0
if quadrant == RIGHT_EDGE:
expectedTrips = numTrips
_testQuadrant((90, -180), (90, 180), expectedTrips, "right edge")
expectedTrips = 0
if quadrant == BOTTOM_EDGE:
expectedTrips = numTrips
_testQuadrant((-90, -180), (90, -180), expectedTrips, "bottom edge")
return True
def testOneTrip():
r = initRedis()
trips = []
trip = Trip(0.1)
trips.append(trip)
trip.start()
trip.join()
waitForAllTrips(trips)
results = {
'trips': [],
'fare': 0,
}
QueryRect((-90, -180), (90, 180), "ALL", "-inf", "inf", False, results)
assert(len(results['trips']) == 1), \
"Error, testOneTrip reported %d trips != %d: %r" \
% (len(results['trips']), 1, results['trips'])
log(1, "testOneTrip pass")
return True
def main():
testOneTrip()
testAllQuadrants()
testQuadrant(TOP_LEFT)
testQuadrant(TOP_RIGHT)
testQuadrant(BOTTOM_RIGHT)
testQuadrant(BOTTOM_LEFT)
testQuadrant(LEFT_EDGE)
testQuadrant(TOP_EDGE)
testQuadrant(RIGHT_EDGE)
testQuadrant(BOTTOM_EDGE)
if __name__ == "__main__":
main()