[autotest] Add two rpm util classes
This is the 1st CL for making the rpm infra read rpm/outlet/hydra
information from AFE.
PowerUnitInfo: This is a class that wraps all information related
to a dut's power control. It will be passed from frontend_server
to rpm_dispatcher to rpm_controller. It wraps all info in a single
class so that in the future, if we want to pass more information
around, we can simply add stuff to this class without having to modify
frontend_server, rpm_dispatcher, rpm_controller.
LRUCache: A simple lru cache to store PowerUnitInfo instances.
When we got a request for a device (dut/servo), a PowerUnitInfo
instace will be created and populated. For chromeos-duts, we will
retrieve such info from AFE. The LRU is used to store recently used
PowerUnitInfo, so that we don't hit the AFE too often.
TEST=unittest
BUG=chromium:392548
Change-Id: Iec153b6dfa640fc288a1bd664a307745dcd12e12
Reviewed-on: https://chromium-review.googlesource.com/212349
Reviewed-by: Simran Basi <sbasi@chromium.org>
Commit-Queue: Fang Deng <fdeng@chromium.org>
Tested-by: Fang Deng <fdeng@chromium.org>
diff --git a/site_utils/rpm_control_system/utils_unittest.py b/site_utils/rpm_control_system/utils_unittest.py
index b9554ed..567bf81 100755
--- a/site_utils/rpm_control_system/utils_unittest.py
+++ b/site_utils/rpm_control_system/utils_unittest.py
@@ -73,5 +73,32 @@
self._reload_helper(False)
+ def test_LRU_cache(self):
+ """Test LRUCache."""
+ p1 = utils.PowerUnitInfo(
+ 'host1', utils.PowerUnitInfo.POWERUNIT_TYPES.RPM,
+ 'rpm1', 'hydra1')
+ p2 = utils.PowerUnitInfo(
+ 'host2', utils.PowerUnitInfo.POWERUNIT_TYPES.RPM,
+ 'rpm2', 'hydra2')
+ p3 = utils.PowerUnitInfo(
+ 'host3', utils.PowerUnitInfo.POWERUNIT_TYPES.RPM,
+ 'rpm3', 'hydra3')
+ # Initialize an LRU with size 2.
+ cache = utils.LRUCache(2)
+ # Add two items, LRU should be full now
+ cache['host1'] = p1
+ cache['host2'] = p2
+ self.assertEqual(len(cache.cache), 2)
+ # Visit host2 and add one more item
+ # host1 should be removed from cache
+ _ = cache['host2']
+ cache['host3'] = p3
+ self.assertEqual(len(cache.cache), 2)
+ self.assertTrue('host1' not in cache)
+ self.assertTrue('host2' in cache)
+ self.assertTrue('host3' in cache)
+
+
if __name__ == '__main__':
unittest.main()