import pytest
from cps2_zmq.gather import MameSink

@pytest.mark.parametrize("message, expected",[
    ({'wid': 420, 'message': 'closing'}, 'worksink closing'),
    ({'wid': 420, 'message': 'threaddead'}, '420 is dead'),
    ({'wid': 420, 'message': 'some result'}, 'another message'),
])
def test_process_message(message, expected, worker):
    sink = MameSink.MameSink("inproc://help")
    worker.wid = message['wid']
    sink.setup_workers2([worker])

    result = sink._process_message(message)
    assert result == expected
    sink._cleanup()

# @pytest.mark.parametrize("messages, expected", [
#     ([{'frame_number': 1141, 'sprites': [[420, 69, 300, 1], [1, 1, 1, 1]], 'palettes': [[]]},
#       {'frame_number': 0, 'sprites': [], 'palettes': []}], 1)
# ])
# @pytest.mark.timeout(timeout=10, method='thread')
# def test_run(workers, messages, expected):
#     sink = MameSink.MameSink("inproc://frommockworkers")
#     sink.setup_workers2(workers)
#     pass
import pytest
from cps2_zmq.gather import MameSink

@pytest.fixture(scope="module")
def sink():
    sink = MameSink.MameSink("inproc://frommockworkers")
    yield sink
    sink.cleanup()

class TestSink(object):
    @pytest.fixture(autouse=True)
    def refresh(self, sink):
        pass
        yield
        sink._msgsrecv = 0

    @pytest.mark.parametrize("message, expected", [
        ({'wid': 420, 'message': 'closing'}, 'worksink closing'),
        ({'wid': 420, 'message': 'threaddead'}, '420 is dead'),
        ({'wid': 420, 'message': 'some result'}, 'another message'),
    ])
    def test_process_message(self, message, expected, sink, worker):
        worker.wid = message['wid']
        sink.setup_workers2([worker])

        result = sink._process_message(message)
        assert result == expected

    def test_run(self, sink, tworkers):
        # sink = MameSink.MameSink("inproc://frommockworkers")

        messages = ['some result', 'closing', 'threaddead']
        for worker in tworkers:
            worker.messages = [{'wid' : worker.wid, 'message': msg} for msg in messages]
            worker.connect_push("inproc://frommockworkers")

        sink.setup_workers2(tworkers)

        sink.start()
        #block and let the sink run
        sink.join()
        assert not sink.workers
        assert sink._msgsrecv == len(tworkers) * len(messages)
    
Test Class now returns to base state between different groups of tests
Test Class now returns to base state between different groups of tests

  import pytest
  from cps2_zmq.gather import MameSink
  
+ @pytest.fixture(scope="module")
+ def sink():
- @pytest.mark.parametrize("message, expected",[
-     ({'wid': 420, 'message': 'closing'}, 'worksink closing'),
-     ({'wid': 420, 'message': 'threaddead'}, '420 is dead'),
-     ({'wid': 420, 'message': 'some result'}, 'another message'),
- ])
- def test_process_message(message, expected, worker):
-     sink = MameSink.MameSink("inproc://help")
+     sink = MameSink.MameSink("inproc://frommockworkers")
-     worker.wid = message['wid']
-     sink.setup_workers2([worker])
+     yield sink
+     sink.cleanup()
  
-     result = sink._process_message(message)
-     assert result == expected
-     sink._cleanup()
+ class TestSink(object):
+     @pytest.fixture(autouse=True)
+     def refresh(self, sink):
+         pass
+         yield
+         sink._msgsrecv = 0
  
- # @pytest.mark.parametrize("messages, expected", [
+     @pytest.mark.parametrize("message, expected", [
- #     ([{'frame_number': 1141, 'sprites': [[420, 69, 300, 1], [1, 1, 1, 1]], 'palettes': [[]]},
- #       {'frame_number': 0, 'sprites': [], 'palettes': []}], 1)
- # ])
- # @pytest.mark.timeout(timeout=10, method='thread')
- # def test_run(workers, messages, expected):
+         ({'wid': 420, 'message': 'closing'}, 'worksink closing'),
+         ({'wid': 420, 'message': 'threaddead'}, '420 is dead'),
+         ({'wid': 420, 'message': 'some result'}, 'another message'),
+     ])
+     def test_process_message(self, message, expected, sink, worker):
+         worker.wid = message['wid']
+         sink.setup_workers2([worker])
+ 
+         result = sink._process_message(message)
+         assert result == expected
+ 
+     def test_run(self, sink, tworkers):
- #     sink = MameSink.MameSink("inproc://frommockworkers")
+         # sink = MameSink.MameSink("inproc://frommockworkers")
+ 
+         messages = ['some result', 'closing', 'threaddead']
+         for worker in tworkers:
+             worker.messages = [{'wid' : worker.wid, 'message': msg} for msg in messages]
+             worker.connect_push("inproc://frommockworkers")
+ 
- #     sink.setup_workers2(workers)
+         sink.setup_workers2(tworkers)
- #     pass
+ 
+         sink.start()
+         #block and let the sink run
+         sink.join()
+         assert not sink.workers
+         assert sink._msgsrecv == len(tworkers) * len(messages)
+     
Test Class now returns to base state between different groups of tests
## Code Before:
import pytest
from cps2_zmq.gather import MameSink

@pytest.mark.parametrize("message, expected",[
    ({'wid': 420, 'message': 'closing'}, 'worksink closing'),
    ({'wid': 420, 'message': 'threaddead'}, '420 is dead'),
    ({'wid': 420, 'message': 'some result'}, 'another message'),
])
def test_process_message(message, expected, worker):
    sink = MameSink.MameSink("inproc://help")
    worker.wid = message['wid']
    sink.setup_workers2([worker])

    result = sink._process_message(message)
    assert result == expected
    sink._cleanup()

# @pytest.mark.parametrize("messages, expected", [
#     ([{'frame_number': 1141, 'sprites': [[420, 69, 300, 1], [1, 1, 1, 1]], 'palettes': [[]]},
#       {'frame_number': 0, 'sprites': [], 'palettes': []}], 1)
# ])
# @pytest.mark.timeout(timeout=10, method='thread')
# def test_run(workers, messages, expected):
#     sink = MameSink.MameSink("inproc://frommockworkers")
#     sink.setup_workers2(workers)
#     pass
## Instruction:
Test Class now returns to base state between different groups of tests
## Code After:
import pytest
from cps2_zmq.gather import MameSink

@pytest.fixture(scope="module")
def sink():
    sink = MameSink.MameSink("inproc://frommockworkers")
    yield sink
    sink.cleanup()

class TestSink(object):
    @pytest.fixture(autouse=True)
    def refresh(self, sink):
        pass
        yield
        sink._msgsrecv = 0

    @pytest.mark.parametrize("message, expected", [
        ({'wid': 420, 'message': 'closing'}, 'worksink closing'),
        ({'wid': 420, 'message': 'threaddead'}, '420 is dead'),
        ({'wid': 420, 'message': 'some result'}, 'another message'),
    ])
    def test_process_message(self, message, expected, sink, worker):
        worker.wid = message['wid']
        sink.setup_workers2([worker])

        result = sink._process_message(message)
        assert result == expected

    def test_run(self, sink, tworkers):
        # sink = MameSink.MameSink("inproc://frommockworkers")

        messages = ['some result', 'closing', 'threaddead']
        for worker in tworkers:
            worker.messages = [{'wid' : worker.wid, 'message': msg} for msg in messages]
            worker.connect_push("inproc://frommockworkers")

        sink.setup_workers2(tworkers)

        sink.start()
        #block and let the sink run
        sink.join()
        assert not sink.workers
        assert sink._msgsrecv == len(tworkers) * len(messages)
    