Skip to content

Commit

Permalink
Allow subclassing with extra args.
Browse files Browse the repository at this point in the history
  • Loading branch information
ionelmc committed Oct 23, 2018
1 parent 2052fce commit d9a4898
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/stampede/worker.py
Expand Up @@ -42,13 +42,13 @@ def __str__(self):
class SingleInstanceMeta(type):
__inst = None

def __call__(cls, path):
def __call__(cls, path, *args, **kwargs):
if cls.__inst is not None:
raise RuntimeError("Only 1 instance allowed!")

lock = FileLock(path)
if lock.acquire():
inst = cls.__inst = super(SingleInstanceMeta, cls).__call__(path)
inst = cls.__inst = super(SingleInstanceMeta, cls).__call__(path, *args, **kwargs)
return inst
else:
return StampedeStub()
Expand Down
14 changes: 14 additions & 0 deletions tests/test_worker.py
Expand Up @@ -178,6 +178,20 @@ def test_empty_request():

def test_double_instance():
from stampede import StampedeWorker
StampedeWorker.__SingleInstanceMeta_inst = None
StampedeWorker(helper.PATH)
with pytest.raises(RuntimeError):
StampedeWorker(helper.PATH)


def test_subclassing():
from stampede import StampedeWorker

class MyWorker(StampedeWorker):
def __init__(self, path, config):
super(MyWorker, self).__init__(path)
self.config = config

MyWorker.__SingleInstanceMeta_inst = None
worker = MyWorker(helper.PATH, 'foobar')
assert worker.config == 'foobar'

0 comments on commit d9a4898

Please sign in to comment.