Пример #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()