summaryrefslogtreecommitdiff
path: root/slog/counter_tests.py
blob: 2c19fe1bd19c182483ca73f60b524f8d19d0d054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Copyright 2016 QvarnLabs Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import threading
import unittest

import slog


class CounterTests(unittest.TestCase):

    def test_counts_to_a_million_in_ten_threads(self):
        max_count = 100000
        num_threads = 2
        counter = slog.Counter()

        threads = []
        for _ in range(num_threads):
            ct = CountThread(max_count, counter)
            ct.start()
            threads.append(ct)
        for ct in threads:
            ct.join()

        self.assertEqual(counter.get(), max_count * num_threads)


class CountThread(threading.Thread):

    def __init__(self, n, counter):
        super(CountThread, self).__init__()
        self.n = n
        self.counter = counter

    def run(self):
        for _ in range(self.n):
            self.counter.increment()