예제 #1
0
 def test_thread(self):
     """Test a thread accessing the shared dict."""
     action_stats = ActionStatistics()
     thread = threading.Thread(target=self.sub_process,
                               args=(action_stats, [{
                                   "action": "jump",
                                   "time": 100
                               }, {
                                   "action": "run",
                                   "time": 75
                               }, {
                                   "action": "jump",
                                   "time": 200
                               }], 0))
     thread.start()
     thread.join()
     sleep(2)
     assert action_stats._dict == {
         'jump': {
             'total': 100 + 200,
             'count': 2
         },
         'run': {
             'total': 75,
             'count': 1
         }
     }
     assert action_stats.getStats() == dumps([{
         "action": "jump",
         "avg": 150.0
     }, {
         "action": "run",
         "avg": 75.0
     }])
예제 #2
0
 def test_getStats(self):
     """Test getting an item."""
     action_stats = ActionStatistics()
     action_stats._dict = {'jump': {'total': 100, 'count': 1}}
     assert loads(action_stats.getStats()) == [{
         "action": "jump",
         "avg": 100.0
     }]
예제 #3
0
 def test_addAction_multiple(self):
     """Test that the average is being calculated correctly."""
     action_stats = ActionStatistics()
     action_stats.addAction('''{"action":"jump", "time":100}''')
     action_stats.addAction('''{"action":"jump", "time":200}''')
     sleep(2)
     assert action_stats.getStats(
     ) == '''[{"action": "jump", "avg": 150.0}]'''
예제 #4
0
 def test_get_stats(self):
     """Test the internal _get_stats method."""
     action_stats = ActionStatistics()
     action_stats._dict = {'jump': {'total': 100, 'count': 1}}
     assert loads(action_stats._get_stats()) == [{
         "action": "jump",
         "avg": 100
     }]
예제 #5
0
    def sub_process(action_stats: ActionStatistics,
                    add_action: dict,
                    sleep_window=0):
        """Action to be taken by a subprocess.

        Parameter
        ---------
        action_stats : ActionStatistics
            Class to utilize.
        add_action : dict
            Action to add to the shared dict.
        sleep_window : int
            How long to sleep before taking any action. This is a positive integer value.
        """
        if sleep_window > 0:
            sleep(randint(1, sleep_window))

        for action in add_action:
            action_stats.addAction(dumps(action))
예제 #6
0
        def trial_run():
            _action_stats = ActionStatistics()
            threads = list()
            for d in [[{'action': 'jump', 'time': x}] for x in range(100)]:
                t = threading.Thread(target=self.sub_process,
                                     args=(_action_stats, d, 5))
                threads.append(t)

            for t in threads:
                t.start()
            for t in threads:
                t.join()

            sleep(10)
            assert _action_stats._dict['jump']['total'] / _action_stats._dict[
                'jump']['count'] == 49.5
            return _action_stats
예제 #7
0
 def test_addAction(self):
     """Test adding an item."""
     action_stats = ActionStatistics()
     action_stats.addAction('''{"action":"jump", "time":100}''')
     sleep(2)
     assert action_stats._dict == {'jump': {'total': 100, 'count': 1}}
예제 #8
0
 def test_add_action(self):
     """Test the internal _add_action method."""
     action_stats = ActionStatistics()
     action_stats._add_action('''{"action":"jump", "time":100}''')
     action_stats._add_action('''{"action":"jump", "time":200}''')
     assert action_stats._dict == {'jump': {'total': 300, 'count': 2}}
예제 #9
0
 def test_empty(self):
     """Test accessing the shared dictionary."""
     action_stats = ActionStatistics()
     assert action_stats.getStats() == '[]'
예제 #10
0
 def test_init(self):
     """Test the most basic init of the ActionStatistics class."""
     ActionStatistics()