Skip to content
This repository has been archived by the owner on Mar 13, 2022. It is now read-only.

Commit

Permalink
common thread lock for MockResourceLock
Browse files Browse the repository at this point in the history
  • Loading branch information
Invictus17 committed Jan 13, 2021
1 parent 1a674df commit 1771cd4
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions leaderelection/leaderelection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import time
import pytest

thread_lock = threading.RLock()

class LeaderElectionTest(unittest.TestCase):
def test_simple_leader_election(self):
election_history = []
Expand All @@ -38,7 +40,7 @@ def on_update():
def on_change():
election_history.append("change record")

mock_lock = MockResourceLock("mock", "mock_namespace", "mock", on_create, on_update, on_change, None)
mock_lock = MockResourceLock("mock", "mock_namespace", "mock", thread_lock, on_create, on_update, on_change, None)

def on_started_leading():
leadership_history.append("start leading")
Expand Down Expand Up @@ -71,7 +73,7 @@ def on_update_A():
def on_change_A():
election_history.append("A gets leadership")

mock_lock_A = MockResourceLock("mock", "mock_namespace", "MockA", on_create_A, on_update_A, on_change_A, None)
mock_lock_A = MockResourceLock("mock", "mock_namespace", "MockA", thread_lock, on_create_A, on_update_A, on_change_A, None)
mock_lock_A.renew_count_max = 3

def on_started_leading_A():
Expand All @@ -94,7 +96,7 @@ def on_update_B():
def on_change_B():
leadership_history.append("B gets leadership")

mock_lock_B = MockResourceLock("mock", "mock_namespace", "MockB", on_create_B, on_update_B, on_change_B, None)
mock_lock_B = MockResourceLock("mock", "mock_namespace", "MockB", thread_lock, on_create_B, on_update_B, on_change_B, None)
mock_lock_B.renew_count_max = 4

def on_started_leading_B():
Expand Down Expand Up @@ -164,7 +166,7 @@ def on_change():
def on_try_update():
election_history.append("try update record")

mock_lock = MockResourceLock("mock", "mock_namespace", "mock", on_create, on_update, on_change, on_try_update)
mock_lock = MockResourceLock("mock", "mock_namespace", "mock", thread_lock, on_create, on_update, on_change, on_try_update)
mock_lock.renew_count_max = 3

def on_started_leading():
Expand Down Expand Up @@ -204,14 +206,15 @@ def assert_history(self, history, expected):


class MockResourceLock:
def __init__(self, name, namespace, identity, on_create=None, on_update=None, on_change=None, on_try_update=None):
def __init__(self, name, namespace, identity, shared_lock, on_create=None, on_update=None, on_change=None, on_try_update=None):
# self.leader_record is shared between two MockResourceLock objects
self.leader_record = []
self.renew_count = 0
self.renew_count_max = 4
self.name = name
self.namespace = namespace
self.identity = str(identity)
self.lock = threading.RLock()
self.lock = shared_lock

self.on_create = on_create
self.on_update = on_update
Expand Down

0 comments on commit 1771cd4

Please sign in to comment.