Skip to content

Commit d9c4b92

Browse files
author
wallacbe
authored
add cached parameters (#955)
1 parent fdc5842 commit d9c4b92

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

openhtf/output/callbacks/mfg_inspector.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class MfgInspector(object):
125125
SCOPE_CODE_URI = 'https://www.googleapis.com/auth/glass.infra.quantum_upload'
126126
DESTINATION_URL = ('https://clients2.google.com/factoryfactory/'
127127
'uploads/quantum_upload/?json')
128+
PARAMS = ['dut_id', 'end_time_millis', 'start_time_millis', 'station_id']
128129

129130
# These attributes control format of callback and what actions are undertaken
130131
# when called. These should either be set by a subclass or via configure.
@@ -158,6 +159,7 @@ def __init__(self, user=None, keydata=None,
158159
self.upload_result = None
159160

160161
self._cached_proto = None
162+
self._cached_params = dict.fromkeys(self.PARAMS)
161163

162164
@classmethod
163165
def from_json(cls, json_data):
@@ -177,12 +179,19 @@ def from_json(cls, json_data):
177179
keydata=json_data['private_key'],
178180
token_uri=json_data['token_uri'])
179181

182+
def _check_cached_params(self, test_record_obj):
183+
"""Check if all cached params equal the values in test record."""
184+
for param in self.PARAMS:
185+
if self._cached_params[param] != getattr(test_record_obj, param):
186+
return False
187+
return True
188+
180189
def _convert(self, test_record_obj):
181190
"""Convert and cache a test record to a mfg-inspector proto."""
182-
183-
if self._cached_proto is None:
191+
if self._cached_proto is None or not self._check_cached_params(test_record_obj):
184192
self._cached_proto = self._converter(test_record_obj)
185-
193+
for param in self.PARAMS:
194+
self._cached_params[param] = getattr(test_record_obj, param)
186195
return self._cached_proto
187196

188197
def save_to_disk(self, filename_pattern=None):

test/output/callbacks/mfg_inspector_test.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
actually care for.
1919
"""
2020

21+
import collections
2122
import io
2223
import unittest
2324

@@ -40,6 +41,9 @@
4041
test_info=test_runs_pb2.TestInfo(name='unit_test')
4142
)
4243

44+
MOCK_TEST_RUN = collections.namedtuple(
45+
'Testrun', mfg_inspector.MfgInspector.PARAMS)(None, None, None, None)
46+
4347

4448
class TestMfgInspector(test.TestCase):
4549

@@ -100,7 +104,7 @@ def test_upload_only(self):
100104
user='user', keydata='keydata', token_uri='').set_converter(
101105
mock_converter)
102106

103-
callback.upload()({})
107+
callback.upload()(MOCK_TEST_RUN)
104108

105109
self.mock_send_mfg_inspector_data.assert_called_with(
106110
MOCK_TEST_RUN_PROTO, self.mock_credentials, callback.destination_url)
@@ -113,8 +117,8 @@ def test_save_and_upload(self):
113117
user='user', keydata='keydata', token_uri='')
114118
callback.set_converter(mock_converter)
115119

116-
callback.save_to_disk(filename_pattern=testrun_output)({})
117-
callback.upload()({})
120+
callback.save_to_disk(filename_pattern=testrun_output)(MOCK_TEST_RUN)
121+
callback.upload()(MOCK_TEST_RUN)
118122

119123
# Parse what was written to BytesIO back into a proto and compare
120124
testrun_output.seek(0)

0 commit comments

Comments
 (0)