Skip to content

Commit 1fe6c07

Browse files
committed
Fixed issue on database when requesting latest headers. Updated test case.
1 parent 3543412 commit 1fe6c07

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

mocha_core/mocha_core/database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ def get_ts_dict(self):
171171
ts_dict[robot_id] = {}
172172
for topic in self.db[robot_id]:
173173
if topic not in ts_dict[robot_id]:
174-
ts_dict[robot_id][topic] = -np.inf
174+
ts_dict[robot_id][topic] = None
175175
for header in self.db[robot_id][topic]:
176176
msg = self.db[robot_id][topic][header]
177-
ts_dict[robot_id][topic] = max(ts_dict[robot_id][topic],
178-
msg.ts.nanoseconds)
177+
curr_time = ts_dict[robot_id][topic]
178+
ts_dict[robot_id][topic] = max(curr_time, msg.ts) if curr_time is not None else msg.ts
179179
self.lock.release()
180180
return ts_dict
181181

mocha_core/test/test_database.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sample_db
99
import mocha_core.hash_comm as hc
1010
import mocha_core.database_utils as du
11+
import copy
1112

1213
import pprint
1314

@@ -76,16 +77,53 @@ def test_get_header_list(self):
7677
def test_headers_not_in_local(self):
7778
dbl = sample_db.get_sample_dbl()
7879
header_list = dbl.get_header_list()
80+
# Test with all headers
7981
extra_header_1 = du.generate_random_header()
8082
extra_header_2 = du.generate_random_header()
8183
header_list.append(extra_header_2)
8284
header_list.append(extra_header_1)
8385
new_headers = [extra_header_1, extra_header_2]
8486
new_headers.sort()
87+
# Test without newer
8588
discover_extra_header = dbl.headers_not_in_local(header_list)
8689
discover_extra_header.sort()
8790
self.assertListEqual(discover_extra_header,
8891
new_headers)
92+
# Test with older header, create a copy of one of the existing headers in the
93+
# db. Pick one with the msec field that can accomodate both addition and
94+
# substraction
95+
test_header = None
96+
for header in header_list:
97+
h = hc.TsHeader.from_header(header)
98+
if h.msecs > 0 and h.msecs < 999:
99+
test_header = header
100+
break
101+
self.assertTrue(test_header is not None)
102+
# Modify the timestamp to make it before the current msg
103+
h = hc.TsHeader.from_header(test_header)
104+
h.msecs -= 1
105+
extra_header_3 = h.bindigest()
106+
header_list.append(extra_header_3)
107+
discover_extra_header_latest = dbl.headers_not_in_local(header_list, newer=True)
108+
discover_extra_header_latest.sort()
109+
discover_extra_header_all = dbl.headers_not_in_local(header_list)
110+
discover_extra_header_all.sort()
111+
self.assertListEqual(discover_extra_header_latest, new_headers)
112+
diff_header = set(discover_extra_header_all) - set(discover_extra_header)
113+
self.assertEqual(diff_header.pop(), extra_header_3)
114+
# Modify the timestamp to make it after the current msg
115+
h = hc.TsHeader.from_header(test_header)
116+
h.msecs += 1
117+
extra_header_4 = h.bindigest()
118+
header_list.append(extra_header_4)
119+
discover_extra_header_latest = dbl.headers_not_in_local(header_list, newer=True)
120+
new_headers.append(extra_header_4) # The new header should show in the list
121+
new_headers.sort()
122+
discover_extra_header_latest.sort()
123+
self.assertListEqual(new_headers, discover_extra_header_latest)
124+
# Finally, getting all the headers should not filter them out
125+
discover_extra_header = dbl.headers_not_in_local(header_list)
126+
self.assertTrue(len(discover_extra_header) == 4)
89127

90128
def test_find_header(self):
91129
dbl = sample_db.get_sample_dbl()

0 commit comments

Comments
 (0)