예제 #1
0
    def test_default_state():

        fake_request(10.0)

        assert pmnc.state.get("") is None
        assert pmnc.state.get("", "default") == "default"

        pmnc.state.set("", None)
        assert pmnc.state.get("") is None
        assert pmnc.state.get("", "default") is None

        pmnc.state.delete("")
        assert pmnc.state.get("", "default") == "default"

        # and now for str/bytes support

        pmnc.state.set(rus, rus + rus)
        assert pmnc.state.get(rus) == rus + rus
        pmnc.state.delete(rus)

        with expected(InputParameterError):
            pmnc.state.set(rus_b, rus)

        with expected(InputParameterError):
            pmnc.state.get(rus_b)

        with expected(InputParameterError):
            pmnc.state.delete(rus_b)

        pmnc.state.set(rus, rus_b)
        assert pmnc.state.get(rus) == rus_b
        pmnc.state.delete(rus)
예제 #2
0
    def test_bytes_str():

        fake_request(5.0)

        xa = pmnc.transaction.create()
        xa.state.set(rus, rus + rus)
        assert xa.execute() == (None, )

        xa = pmnc.transaction.create()
        xa.state.get(rus)
        assert xa.execute() == (rus + rus, )

        xa = pmnc.transaction.create()
        xa.state.delete(rus)
        assert xa.execute() == (None, )

        xa = pmnc.transaction.create()
        xa.state.get(rus_b)
        with expected(ResourceInputParameterError):
            xa.execute()

        xa = pmnc.transaction.create()
        xa.state.set(rus_b, "value")
        with expected(ResourceInputParameterError):
            xa.execute()

        xa = pmnc.transaction.create()
        xa.state.delete(rus_b)
        with expected(ResourceInputParameterError):
            xa.execute()
예제 #3
0
파일: state.py 프로젝트: Tombar/pythomnic3k
    def test_default_state():

        fake_request(10.0)

        assert pmnc.state.get("") is None
        assert pmnc.state.get("", "default") == "default"

        pmnc.state.set("", None)
        assert pmnc.state.get("") is None
        assert pmnc.state.get("", "default") is None

        pmnc.state.delete("")
        assert pmnc.state.get("", "default") == "default"

        # and now for str/bytes support

        pmnc.state.set(rus, rus + rus)
        assert pmnc.state.get(rus) == rus + rus
        pmnc.state.delete(rus)

        with expected(InputParameterError):
            pmnc.state.set(rus_b, rus)

        with expected(InputParameterError):
            pmnc.state.get(rus_b)

        with expected(InputParameterError):
            pmnc.state.delete(rus_b)

        pmnc.state.set(rus, rus_b)
        assert pmnc.state.get(rus) == rus_b
        pmnc.state.delete(rus)
예제 #4
0
    def test_bytes_str():

        fake_request(5.0)

        xa = pmnc.transaction.create()
        xa.state.set(rus, rus + rus)
        assert xa.execute() == (None,)

        xa = pmnc.transaction.create()
        xa.state.get(rus)
        assert xa.execute() == (rus + rus,)

        xa = pmnc.transaction.create()
        xa.state.delete(rus)
        assert xa.execute() == (None,)

        xa = pmnc.transaction.create()
        xa.state.get(rus_b)
        with expected(ResourceInputParameterError):
            xa.execute()

        xa = pmnc.transaction.create()
        xa.state.set(rus_b, "value")
        with expected(ResourceInputParameterError):
            xa.execute()

        xa = pmnc.transaction.create()
        xa.state.delete(rus_b)
        with expected(ResourceInputParameterError):
            xa.execute()
예제 #5
0
    def test_bind_points():

        db_config = pmnc.config_resource_postgresql_2.copy()
        assert db_config.pop("protocol") == "postgresql_psycopg"
        r = Resource("test", **db_config)

        assert r._convert_bind_points("", {}) == ("", {})
        assert r._convert_bind_points("{i}", dict(i = 123)) == ("%(i)s", dict(i = 123))
        assert r._convert_bind_points("{{i}}{i}{{i}}", dict(i = 123)) == ("{i}%(i)s{i}", dict(i = 123))
        assert r._convert_bind_points("{i}{i}", dict(i = 123)) == ("%(i)s%(i)s", dict(i = 123))
        assert r._convert_bind_points("{i}{{i}}{i}", dict(i = 123)) == ("%(i)s{i}%(i)s", dict(i = 123))
        assert r._convert_bind_points("{i}{{i{i}i}}{i}", dict(i = 123)) == ("%(i)s{i%(i)si}%(i)s", dict(i = 123))

        assert r._convert_bind_points("", dict(i = 123, s = "foo")) == ("", dict())
        assert r._convert_bind_points("{i}", dict(i = 123, s = "foo")) == ("%(i)s", dict(i = 123))
        assert r._convert_bind_points("{s}", dict(i = 123, s = "foo")) == ("%(s)s", dict(s = "foo"))
        assert r._convert_bind_points("{i}{s}", dict(i = 123, s = "foo")) == ("%(i)s%(s)s", dict(i = 123, s = "foo"))
        assert r._convert_bind_points("{s}{i}", dict(i = 123, s = "foo")) == ("%(s)s%(i)s", dict(i = 123, s = "foo"))
        assert r._convert_bind_points("{i}{s}{i}", dict(i = 123, s = "foo")) == ("%(i)s%(s)s%(i)s", dict(i = 123, s = "foo"))
        assert r._convert_bind_points("{s}{i}{s}", dict(i = 123, s = "foo")) == ("%(s)s%(i)s%(s)s", dict(i = 123, s = "foo"))

        assert r._convert_bind_points("{x} {{}} {y} {{}} {z} " * 10000, dict(x = None, y = 123, z = "foo")) == \
                                   ("%(x)s {} %(y)s {} %(z)s " * 10000, dict(x = None, y = 123, z = "foo"))

        with expected(KeyError("missing")):
            r._convert_bind_points("{missing}", {})

        with expected(ValueError("Unknown ")):
            r._convert_bind_points("{i:d}", dict(i = 123))
예제 #6
0
    def test_qmarks():

        db_config = pmnc.config_resource_postgresql_1.copy()
        assert db_config.pop("protocol") == "postgresql_pg8000"
        r = Resource("test", **db_config)

        assert r._convert_to_qmarks("", {}) == ("", ())
        assert r._convert_to_qmarks("{i}", dict(i = 123)) == ("?", (123,))
        assert r._convert_to_qmarks("{{i}}{i}{{i}}", dict(i = 123)) == ("{i}?{i}", (123,))
        assert r._convert_to_qmarks("{i}{i}", dict(i = 123)) == ("??", (123, 123))
        assert r._convert_to_qmarks("{i}{{i}}{i}", dict(i = 123)) == ("?{i}?", (123, 123))
        assert r._convert_to_qmarks("{i}{{i{i}i}}{i}", dict(i = 123)) == ("?{i?i}?", (123, 123, 123))

        assert r._convert_to_qmarks("", dict(i = 123, s = "foo")) == ("", ())
        assert r._convert_to_qmarks("{i}", dict(i = 123, s = "foo")) == ("?", (123,))
        assert r._convert_to_qmarks("{s}", dict(i = 123, s = "foo")) == ("?", ("foo",))
        assert r._convert_to_qmarks("{i}{s}", dict(i = 123, s = "foo")) == ("??", (123, "foo"))
        assert r._convert_to_qmarks("{s}{i}", dict(i = 123, s = "foo")) == ("??", ("foo", 123))
        assert r._convert_to_qmarks("{i}{s}{i}", dict(i = 123, s = "foo")) == ("???", (123, "foo", 123))
        assert r._convert_to_qmarks("{s}{i}{s}", dict(i = 123, s = "foo")) == ("???", ("foo", 123, "foo"))
        assert r._convert_to_qmarks("|{s}|{i}|{c}|", dict(i = 123, s = "foo", c = b"")) == ("|?|?|?|", ("foo", 123, b""))

        assert r._convert_to_qmarks("{x} ? {y} ? {z} " * 10000, dict(x = None, y = 123, z = "foo")) == \
                                   ("? ? ? ? ? " * 10000, (None, 123, "foo") * 10000)

        with expected(KeyError("missing")):
            r._convert_to_qmarks("{missing}", {})

        with expected(ValueError("Unknown ")):
            r._convert_to_qmarks("{i:d}", dict(i = 123))
예제 #7
0
    def test_fragmentation():

        assert Resource._frag_message(b"") == [ b"" ]

        # --- 7 bit encoding ---

        # left padding to septet

        assert Resource._frag_message(b"\x11", 7, 140, 0) == [ b"\x11" ]
        assert Resource._frag_message(b"\xff", 7, 140, 1) == [ b"\x03\xfc" ]
        assert Resource._frag_message(b"\x5a\x5a", 7, 140, 2) == [ b"\x02\xd2\xd0" ]
        assert Resource._frag_message(b"\x01\x02\x04", 7, 140, 3) == [ b"\x00\x10\x20\x40" ]
        assert Resource._frag_message(b"\xfe", 7, 140, 4) == [ b"\x1f\xc0" ]
        assert Resource._frag_message(b"\xff\xff\xff", 7, 140, 5) == [ b"\x3f\xff\xff\xc0" ]
        assert Resource._frag_message(b"\x01\x80", 7, 140, 6) == [ b"\x00\xc0\x00" ]
        assert Resource._frag_message(b"\x11", 7, 140, 7) == [ b"\x11" ]

        # fragmentation

        assert Resource._frag_message(b"\x01\x02\x03\x04\x05\x06\x07", 7, 7) == \
               [ b"\x01\x02\x03\x04\x05\x06\x07" ]

        assert Resource._frag_message(b"\x01\x02\x03\x04\x05\x06\x07\x08", 7, 7) == \
               [ b"\x01\x02\x03\x04\x05\x06\x07", b"\x08" ]

        with expected(Exception("fragment size 8 is too small")):
            Resource._frag_message(b"\xff", 7, 8, 1)

        assert Resource._frag_message(b"\xff\xff\xff\xff\xff\xff\xff", 7, 9, 1) == \
               [ b"\x03\xff\xff\xff\xff\xff\xff\xfc" ]

        assert Resource._frag_message(b"\xff\xff\xff\xff\xff\xff\xff\xff", 7, 9, 1) == \
               [ b"\x03\xff\xff\xff\xff\xff\xff\xfc", b"\x03\xfc" ]

        # --- 8 bit encoding ---

        assert Resource._frag_message(b"\x01\x02", 8, 2) == \
               [ b"\x01\x02" ]

        assert Resource._frag_message(b"\x01\x02", 8, 2, 1) == \
               [ b"\x01", b"\x02" ]

        # --- 16 bit encoding ---

        assert Resource._frag_message(b"\x12\x34", 16) == [ b"\x12\x34" ]

        with expected(Exception("fragment size 1 is too small")):
            Resource._frag_message(b"\x12\x34", 16, 1)

        with expected(Exception("fragment size 2 is too small")):
            Resource._frag_message(b"\x12\x34", 16, 2, 1)

        assert Resource._frag_message(b"\x01\x02\x03\x04", 16, 3) == \
               [ b"\x01\x02", b"\x03\x04" ]
예제 #8
0
    def test_event():

        with expected(InputParameterError("event() has got an incompatible value for key: 123")):
            pmnc.performance.event(123)

        with expected(InputParameterError("event() has got an incompatible value for key: $$$")):
            pmnc.performance.event("$$$")

        with expected(InputParameterError("event() has got an incompatible value for key: foo")):
            pmnc.performance.event("foo")

        pmnc.performance.event("interface.foo.request_rate")
예제 #9
0
    def test_event():

        with expected(InputParameterError("event() has got an incompatible value for key: 123")):
            pmnc.performance.event(123)

        with expected(InputParameterError("event() has got an incompatible value for key: $$$")):
            pmnc.performance.event("$$$")

        with expected(InputParameterError("event() has got an incompatible value for key: foo")):
            pmnc.performance.event("foo")

        pmnc.performance.event("interface.foo.request_rate")
예제 #10
0
    def test_WorkSource_end_work():

        ws = _WorkSource(2.0)
        with expected(AssertionError):
            ws.end_work()
        ws.add_work()
        with expected(AssertionError):
            ws.end_work()
        assert ws.begin_work()
        ws.end_work()
        with expected(AssertionError):
            ws.end_work()
예제 #11
0
    def test_WorkSource_end_work():

        ws = _WorkSource(2.0)
        with expected(AssertionError):
            ws.end_work()
        ws.add_work()
        with expected(AssertionError):
            ws.end_work()
        assert ws.begin_work()
        ws.end_work()
        with expected(AssertionError):
            ws.end_work()
예제 #12
0
    def test_sample():

        with expected(InputParameterError("sample() has got an incompatible value for key: 123")):
            pmnc.performance.sample(123, 1000)

        with expected(InputParameterError("sample() has got an incompatible value for key: $$$")):
            pmnc.performance.sample("$$$", 1000)

        with expected(InputParameterError("sample() has got an incompatible value for key: bar")):
            pmnc.performance.sample("bar", 1000)

        with expected(InputParameterError("sample() has got an incompatible value for value: value")):
            pmnc.performance.sample("resource.bar.processing_time", "value")

        pmnc.performance.sample("resource.bar.processing_time", 1000)
예제 #13
0
    def test_sample():

        with expected(InputParameterError("sample() has got an incompatible value for key: 123")):
            pmnc.performance.sample(123, 1000)

        with expected(InputParameterError("sample() has got an incompatible value for key: $$$")):
            pmnc.performance.sample("$$$", 1000)

        with expected(InputParameterError("sample() has got an incompatible value for key: bar")):
            pmnc.performance.sample("bar", 1000)

        with expected(InputParameterError("sample() has got an incompatible value for value: value")):
            pmnc.performance.sample("resource.bar.processing_time", "value")

        pmnc.performance.sample("resource.bar.processing_time", 1000)
예제 #14
0
    def test_transaction_isolation():

        fake_request(10.0)

        tn = "table_{0:s}".format(random_string(8))

        xa = pmnc.transaction.create()
        xa.oracle_1.execute("CREATE TABLE {0:s} (ID NUMBER(8) NOT NULL PRIMARY KEY)".format(tn))
        xa.execute()

        xa = pmnc.transaction.create()
        xa.oracle_1.execute("INSERT INTO {0:s} (ID) VALUES ({{id}})".format(tn),
                            "SELECT ID FROM {0:s}".format(tn), id = 1)
        xa.oracle_1.execute("INSERT INTO {0:s} (ID) VALUES ({{id}})".format(tn),
                            "SELECT ID FROM {0:s}".format(tn), id = 2)
        assert xa.execute() == (([], [{ "ID": 1 }]), ([], [{ "ID": 2 }]))

        fake_request(5.0)

        xa = pmnc.transaction.create()
        xa.oracle_1.execute("INSERT INTO {0:s} (ID) VALUES ({{id}})".format(tn), id = 3) # causes a deadlock
        xa.oracle_1.execute("INSERT INTO {0:s} (ID) VALUES ({{id}})".format(tn), id = 3)
        with expected(Exception("request deadline")):
            xa.execute()

        fake_request(10.0)

        xa = pmnc.transaction.create()
        xa.oracle_1.execute("SELECT id FROM {0:s} ORDER BY id".format(tn),
                            "DROP TABLE {0:s}".format(tn))
        assert xa.execute()[0] == ([{ "ID": 1 }, { "ID": 2 }], [])
예제 #15
0
파일: state.py 프로젝트: Tombar/pythomnic3k
    def test_module_state():

        fake_request(10.0)

        module_state_dir = _module_state_dir("foo")

        module_state = ModuleState(module_state_dir)
        try:

            # note that the first access to a queue or a database creates it,
            # therefore options such as re_len are required, but subsequent
            # accesses just open it and the options can be omitted

            assert not os_path.exists(os_path.join(module_state_dir, "state.queue"))
            queue = module_state.get_queue_db("state", re_len=100)
            assert os_path.exists(os_path.join(module_state_dir, "state.queue"))
            stat = queue.stat()
            assert "buckets" not in stat
            queue.append("foo")
            assert module_state.get_queue_db("state") is queue
            assert module_state.get_queue_db("state2") is not queue

            assert not os_path.exists(os_path.join(module_state_dir, "state.btree"))
            btree = module_state.get_btree_db("state")
            assert os_path.exists(os_path.join(module_state_dir, "state.btree"))
            stat = btree.stat()
            assert "buckets" not in stat
            with expected(bsddb.DBInvalidArgError):
                btree.append("foo")
            assert module_state.get_btree_db("state") is btree
            assert module_state.get_btree_db("state2") is not btree

        finally:
            module_state.close()
예제 #16
0
    def test_execute_timeout():

        fake_request(1.0)

        with expected(Exception("request deadline waiting for response")):
            pmnc.__getattr__(__name__).execute_reverse("bad_cage", "module",
                                                       "method", (), {})
예제 #17
0
    def test_create():

        p = PDUParameter(name="foo",
                         type=SMPPInteger1,
                         check=lambda v: v.value % 2 == 0)

        with expected(
                InputParameterError(
                    "__init__() has got an incompatible value for value: b'bar'"
                )):
            p.create(b"bar")

        assert p.create(0x00) == SMPPInteger1(0)

        with expected(Exception("SMPPInteger1(0x01) does not pass the check")):
            p.create(0x01)
예제 #18
0
    def test_command():

        fake_request(10.0)

        with expected(ResourceError, "no such cmd.*"):
            pmnc.transaction.mongodb_1.command("no-such-cmd")

        # implicit { command: 1 }

        pmnc.transaction.mongodb_1.command("ping")

        # explicit { command: value }

        rs = pmnc.transaction.mongodb_1.command("isMaster", isMaster=1.0)
        assert rs.documents[0]["ismaster"] in (True, False)

        # with extra params

        rs = pmnc.transaction.mongodb_1.command("collStats", {"scale": 1024},
                                                collStats="test")
        assert rs.documents[0]["count"] == 100

        rs = pmnc.transaction.mongodb_1.command("distinct", {
            "key": "k",
            "query": {}
        },
                                                distinct="test")
        vs = rs.documents[0]["values"]
        vs.sort()
        assert vs == list(range(100))
예제 #19
0
    def test_wait_response():

        pdu = EnquireLinkPDU.create()
        with expected(Exception("timeout waiting for response to EnquireLinkPDU")):
            _wait_response(pdu, 1.0)

        resp = pdu.create_response()
        pdu.set_response(resp)
        before = time()
        assert _wait_response(pdu, 0.1) is resp
        after = time()
        assert after - before < 0.01

        resp = pdu.create_nack(error_codes.ESME_RUNKNOWNERR)
        pdu.set_response(resp)
        with expected(SMPPResponseError("SMPP error ESME_RUNKNOWNERR (Unknown Error)")):
            _wait_response(pdu, 0.1)
예제 #20
0
    def test_drop():

        fake_request(10.0)

        pmnc.transaction.mongodb_1.test.drop()

        with expected(ResourceError, ".*not found.*"):
            pmnc.transaction.mongodb_1.command("collStats", {},
                                               collStats="test")
예제 #21
0
    def test_request_processing():

        fake_request(1.0)

        with pmnc.performance.request_processing():
            pass

        with expected(ZeroDivisionError):
            with pmnc.performance.request_processing():
                1 / 0
예제 #22
0
    def test_request_processing():

        fake_request(1.0)

        with pmnc.performance.request_processing():
            pass

        with expected(ZeroDivisionError):
            with pmnc.performance.request_processing():
                1 / 0
예제 #23
0
    def test_read():

        p = PDUParameter(name="foo",
                         type=SMPPInteger1,
                         check=lambda v: v.value % 2 == 0)

        assert p.read(BytesIO(
            SMPPInteger1(0x00).serialize())) == SMPPInteger1(0x00)
        assert p.read(BytesIO(SMPPInteger1(0x00).serialize()),
                      1) == SMPPInteger1(0x00)
        with expected(
                InputParameterError(
                    "read() has got an incompatible value for length: 2")):
            p.read(BytesIO(SMPPInteger1(0x00).serialize()), 2)
        with expected(Exception("SMPPInteger1(0x01) does not pass the check")):
            p.read(BytesIO(SMPPInteger1(0x01).serialize()))

        p = PDUParameter(name="foo", type=SMPPInteger1)
        assert p.read(BytesIO(
            SMPPInteger1(0x01).serialize())) == SMPPInteger1(0x01)
예제 #24
0
    def test_post():

        with expected(Exception("the request is no longer pending for response")):
            pmnc.__getattr__(__name__).post("RQ-ABC", "RESULT")

        rs_queue = InterlockedQueue()
        _rs_queues["RQ-ABC"] = rs_queue

        pmnc.__getattr__(__name__).post("RQ-ABC", "RESULT")

        assert rs_queue.pop() == "RESULT"
예제 #25
0
    def test_close_cursor():

        fake_request(10.0)

        rs = pmnc.transaction.mongodb_1.test.find(docs_to_return=2)
        assert len(rs.documents) == 2 and rs.cursor_id != 0

        pmnc.transaction.mongodb_1.kill_cursors([rs.cursor_id])

        with expected(ResourceError, "cursor not found"):
            pmnc.transaction.mongodb_1.test.get_more(rs.cursor_id)
예제 #26
0
    def test_post():

        with expected(
                Exception("the request is no longer pending for response")):
            pmnc.__getattr__(__name__).post("RQ-ABC", "RESULT")

        rs_queue = InterlockedQueue()
        _rs_queues["RQ-ABC"] = rs_queue

        pmnc.__getattr__(__name__).post("RQ-ABC", "RESULT")

        assert rs_queue.pop() == "RESULT"
예제 #27
0
    def test_convert_to_named():

        db_config = pmnc.config_resource_oracle_1.copy()
        assert db_config.pop("protocol") == "oracle_cx"
        r = Resource("test", **db_config)

        assert r._convert_to_named("", {}) == ("", {})
        assert r._convert_to_named("", { "foo": 1 }) == ("", {})
        assert r._convert_to_named("{foo}", { "foo": 1 }) == (":foo", { "foo": 1 })
        with expected(KeyError("foo")):
            r._convert_to_named("{foo}", {})

        assert r._convert_to_named("{{{foo}}}", { "foo": 1, "bar": 2 }) == ("{:foo}", { "foo": 1 })
        assert r._convert_to_named("{foo}{foo}", { "foo": 1 }) == (":foo:foo", { "foo": 1 })
        assert r._convert_to_named("{foo}:foo:{{foo}}:foo{foo}", { "foo": 1 }) == (":foo:foo:{foo}:foo:foo", { "foo": 1 })
        with expected(KeyError("foo{{foo}}")):
            r._convert_to_named("{{{foo{{foo}}}}}", { "foo": 1 })
        assert r._convert_to_named("{{{foo}{{{foo}}}}}", { "foo": 1, "bar": None }) == ("{:foo{:foo}}", { "foo": 1 })

        assert r._convert_to_named("SELECT {foo} FROM {bar}", { "foo": 1, "bar": 2, "biz": "baz" }) == \
               ("SELECT :foo FROM :bar", { "foo": 1, "bar": 2 })
예제 #28
0
    def test_resource_read():

        fn = random_filename()

        fake_request(3.0)

        pmnc.transaction.file_1.write(fn, b"foobar")

        assert pmnc.transaction.file_1.read(fn) == b"foobar"
        assert os_path.isfile(target_filename(fn))

        assert pmnc.transaction.file_1.read(fn, remove = True, frag_size = 4) == b"foobar"
        assert not os_path.isfile(target_filename(fn))

        with expected(ResourceError, "^.*never_existed.*$"):
            pmnc.transaction.file_1.read("never_existed")

        pmnc.transaction.file_1.write(fn, b"\x00" * 33554432)
        fake_request(0.1)
        with expected(Exception, "^request deadline.*$"):
            pmnc.transaction.file_1.read(fn, frag_size = 1)
예제 #29
0
        def test_timedelta():

            td = timedelta(minutes = 10)
            assert test_value("interval", td) == ([{'value': td}], [], [], [{'value': td}], [])

            td = timedelta(microseconds = 1)
            assert test_value("interval", td) == ([{'value': td}], [], [], [{'value': td}], [])

            td = timedelta(days = 3650, hours = 23, minutes = 59, seconds = 59, microseconds = 999999)
            assert test_value("interval", td) == ([{'value': td}], [], [], [{'value': td}], [])

            with expected(ResourceError, "month intervals cannot be converted"):
                pmnc.transaction.postgresql_1.execute("SELECT '1 month'::interval AS td")
예제 #30
0
    def test_timing():

        with expected(InputParameterError("timing() has got an incompatible value for key: 123")):
            with pmnc.performance.timing(123):
                pass

        with expected(InputParameterError("timing() has got an incompatible value for key: $$$")):
            with pmnc.performance.timing("$$$"):
                pass

        with expected(InputParameterError("timing() has got an incompatible value for key: biz")):
            with pmnc.performance.timing("biz"):
                pass

        with pmnc.performance.timing("interface.biz.processing_time") as t:
            assert t.elapsed_ms < 100
            sleep(1.0)
            assert t.elapsed_ms > 900

        with expected(Exception("foo")):
            with pmnc.performance.timing("interface.biz.processing_time"):
                raise Exception("foo")
예제 #31
0
    def test_timing():

        with expected(InputParameterError("timing() has got an incompatible value for key: 123")):
            with pmnc.performance.timing(123):
                pass

        with expected(InputParameterError("timing() has got an incompatible value for key: $$$")):
            with pmnc.performance.timing("$$$"):
                pass

        with expected(InputParameterError("timing() has got an incompatible value for key: biz")):
            with pmnc.performance.timing("biz"):
                pass

        with pmnc.performance.timing("interface.biz.processing_time") as t:
            assert t.elapsed_ms < 100
            sleep(1.0)
            assert t.elapsed_ms > 900

        with expected(Exception("foo")):
            with pmnc.performance.timing("interface.biz.processing_time"):
                raise Exception("foo")
예제 #32
0
    def test_queue_many_items():

        fake_request(60.0)

        q = pmnc.state.get_queue("queue_many_items", re_len=64)

        counter = InterlockedCounter()

        def _push_n(txn, n):
            for i in range(n):
                c = counter.next()
                _push(txn, q, "test-{0:d}".format(c), pickle)

        def push_n(n):
            pmnc.state.implicit_transaction(_push_n, n)

        # push as much as we can per single transaction

        for i in range(8):
            push_n(1024)
            push_n(2048)
            push_n(4096)
            push_n(8100)

        # until we hit the limit of max_objects (8192) for one transaction

        with expected(MemoryError):
            push_n(8192)

        Timeout(4.0).wait()  # wait for checkpoint

        # now pop everything off the queue

        def _pop_n(txn, n):
            for i in range(n):
                assert _pop(txn, q, None, unpickle) is not None

        def pop_n(n):
            pmnc.state.implicit_transaction(_pop_n, n)

        assert peek(q) == "test-0"

        for i in range(8):
            pop_n(8100)
            pop_n(4096)
            pop_n(2048)
            pop_n(1024)

        # the attempt to push 8192 records should have left no trace

        assert peek(q) is None
예제 #33
0
파일: state.py 프로젝트: Tombar/pythomnic3k
    def test_queue_many_items():

        fake_request(60.0)

        q = pmnc.state.get_queue("queue_many_items", re_len=64)

        counter = InterlockedCounter()

        def _push_n(txn, n):
            for i in range(n):
                c = counter.next()
                _push(txn, q, "test-{0:d}".format(c), pickle)

        def push_n(n):
            pmnc.state.implicit_transaction(_push_n, n)

        # push as much as we can per single transaction

        for i in range(8):
            push_n(1024)
            push_n(2048)
            push_n(4096)
            push_n(8100)

        # until we hit the limit of max_objects (8192) for one transaction

        with expected(MemoryError):
            push_n(8192)

        Timeout(4.0).wait()  # wait for checkpoint

        # now pop everything off the queue

        def _pop_n(txn, n):
            for i in range(n):
                assert _pop(txn, q, None, unpickle) is not None

        def pop_n(n):
            pmnc.state.implicit_transaction(_pop_n, n)

        assert peek(q) == "test-0"

        for i in range(8):
            pop_n(8100)
            pop_n(4096)
            pop_n(2048)
            pop_n(1024)

        # the attempt to push 8192 records should have left no trace

        assert peek(q) is None
예제 #34
0
    def test_deadlock():

        fake_request(10.0)

        tn = random_name()

        pmnc.transaction.mysql_1.execute(
            "CREATE TABLE {0:s} (i INT, PRIMARY KEY (i))".format(tn),
            "INSERT INTO {0:s} (i) VALUES ({{i}})".format(tn), i = 0)

        xa = pmnc.transaction.create()
        xa.mysql_1.execute("SELECT * FROM {0:s} WHERE i = {{zero}} FOR UPDATE".format(tn), zero = 0)
        xa.mysql_1.execute("SELECT * FROM {0:s} WHERE i = {{zero}} FOR UPDATE".format(tn), zero = 0)
        with expected(TransactionExecutionError, "request deadline waiting for intermediate result.*"):
            xa.execute()
예제 #35
0
    def test_resource_kill():

        fake_request(2.0)

        ping = Resource("sleep/1",
                        executable="sleep",
                        arguments=(),
                        environment={})
        ping.connect()
        try:
            ping.set_pool_info("sleep", 1)
            with expected(WorkUnitTimedOut):
                ping.execute(4)
        finally:
            ping.disconnect()
예제 #36
0
    def test_send_one():

        def process_request(request, response):
            pass

        with active_interface("smpp_1", **interface_config(process_request = process_request)):

            sleep(3.0) # to allow connection to spin up

            fake_request(30.0)

            with expected(ResourceError, "^data_coding is not specified, provide short_message of type str$"):
                pmnc.transaction.smpp_1.submit_sm(
                        dest_addr_ton = DEST_TON, dest_addr_npi = DEST_NPI,
                        destination_addr = DEST_ADDR, short_message = b"test")

            with expected(ResourceError, "^data_coding is specified, provide short_message of type bytes$"):
                pmnc.transaction.smpp_1.submit_sm(
                        dest_addr_ton = DEST_TON, dest_addr_npi = DEST_NPI,
                        destination_addr = DEST_ADDR, short_message = "test", data_coding = 0x00)

            pmnc.transaction.smpp_1.submit_sm(
                        dest_addr_ton = DEST_TON, dest_addr_npi = DEST_NPI,
                        destination_addr = DEST_ADDR, short_message = b"test1", data_coding = 0x00)

            pmnc.transaction.smpp_1.submit_sm(
                        dest_addr_ton = DEST_TON, dest_addr_npi = DEST_NPI,
                        destination_addr = DEST_ADDR, short_message = "test2") # encoded to GSM7

            pmnc.transaction.smpp_1.submit_sm(
                        dest_addr_ton = DEST_TON, dest_addr_npi = DEST_NPI,
                        destination_addr = DEST_ADDR, short_message = "@$\\") # encoded to GSM7

            pmnc.transaction.smpp_1.submit_sm(
                        dest_addr_ton = DEST_TON, dest_addr_npi = DEST_NPI,
                        destination_addr = DEST_ADDR, short_message = russian) # encoded to UCS2
    def test_qmarks():

        db_config = pmnc.config_resource_sqlserver_1.copy()
        assert db_config.pop("protocol") == "sqlserver_adodb"
        r = Resource("test", **db_config)

        assert r._convert_to_qmarks("", {}) == ("", ())
        assert r._convert_to_qmarks("{i}", dict(i = 123)) == ("?", (123,))
        assert r._convert_to_qmarks("{i}", dict(i = None)) == ("NULL", ())
        assert r._convert_to_qmarks("{{i}}{i}{{i}}", dict(i = 123)) == ("{i}?{i}", (123,))
        assert r._convert_to_qmarks("{i}{i}", dict(i = 123)) == ("??", (123, 123))
        assert r._convert_to_qmarks("{i}{i}", dict(i = None)) == ("NULLNULL", ())
        assert r._convert_to_qmarks("{i}{{i}}{i}", dict(i = 123)) == ("?{i}?", (123, 123))
        assert r._convert_to_qmarks("{i}{{i{i}i}}{i}", dict(i = 123)) == ("?{i?i}?", (123, 123, 123))
        assert r._convert_to_qmarks("{i}{{i{i}i}}{i}", dict(i = None)) == ("NULL{iNULLi}NULL", ())

        assert r._convert_to_qmarks("", dict(i = 123, s = "foo")) == ("", ())
        assert r._convert_to_qmarks("{i}", dict(i = 123, s = "foo")) == ("?", (123,))
        assert r._convert_to_qmarks("{s}", dict(i = 123, s = "foo")) == ("?", ("foo",))
        assert r._convert_to_qmarks("{i}{s}", dict(i = 123, s = "foo")) == ("??", (123, "foo"))
        assert r._convert_to_qmarks("{s}{i}", dict(i = 123, s = "foo")) == ("??", ("foo", 123))
        assert r._convert_to_qmarks("{i}{s}{i}", dict(i = 123, s = "foo")) == ("???", (123, "foo", 123))
        assert r._convert_to_qmarks("{s}{i}{s}", dict(i = 123, s = "foo")) == ("???", ("foo", 123, "foo"))
        assert r._convert_to_qmarks("|{i}|{s}|{c}|", dict(i = 123, s = "foo", c = b"")) == ("|?|?|?|", (123, "foo", b""))
        assert r._convert_to_qmarks("|{i}|{s}|{c}|", dict(i = None, s = "foo", c = b"")) == ("|NULL|?|?|", ("foo", b""))
        assert r._convert_to_qmarks("|{i}|{s}|{c}|", dict(i = 123, s = None, c = b"")) == ("|?|NULL|?|", (123, b""))
        assert r._convert_to_qmarks("|{i}|{s}|{c}|", dict(i = 123, s = "foo", c = None)) == ("|?|?|NULL|", (123, "foo"))

        assert r._convert_to_qmarks("? {x} ? {y} ? {z} ? " * 10000, dict(x = None, y = 123, z = "foo")) == \
                                   ("? NULL ? ? ? ? ? " * 10000, (123, "foo") * 10000)

        with expected(KeyError("missing")):
            r._convert_to_qmarks("{missing}", {})

        with expected(ValueError("Unknown ")):
            r._convert_to_qmarks("{i:d}", dict(i = 123))
예제 #38
0
def self_test():

    from expected import expected
    from pmnc.request import fake_request
    from pmnc.resource_pool import ResourceError

    fake_request(10.0)

    assert pmnc.transaction.void.success("foo", biz = "baz") == \
           (("foo", ), { "biz": "baz" })

    with expected(ResourceError, "failure"):
        pmnc.transaction.void.failure()

    assert pmnc.transaction.void.execute(lambda *args, **kwargs: (args, kwargs), "foo", biz = "baz") == \
           (("foo", ), { "biz": "baz" })
예제 #39
0
    def test_resource_void():

        fake_request(1.0)

        na = Resource("na/1",
                      executable="never#existed",
                      arguments=(),
                      environment={})
        na.connect()
        try:
            assert na.expired
            na.set_pool_info("na", 1)
            with expected(Exception):
                na.execute()
        finally:
            na.disconnect()
예제 #40
0
    def test_large_queue_items():

        fake_request(10.0)

        q = pmnc.state.get_queue("large_items", re_len=1024, re_pad=0x5a)

        for i in (16, 64, 256, 1024):
            value = b"*" * i
            push(q, value, lambda x: x)

        with expected(bsddb.DBInvalidArgError):
            push(q, b"*" * 1025, lambda x: x)

        for i in (16, 64, 256, 1024):
            value = b"*" * i + b"\x5a" * (1024 - i)
            assert pop(q, None, lambda x: x) == value

        assert pop(q, None, lambda x: x) is None
예제 #41
0
파일: state.py 프로젝트: Tombar/pythomnic3k
    def test_large_queue_items():

        fake_request(10.0)

        q = pmnc.state.get_queue("large_items", re_len=1024, re_pad=0x5A)

        for i in (16, 64, 256, 1024):
            value = b"*" * i
            push(q, value, lambda x: x)

        with expected(bsddb.DBInvalidArgError):
            push(q, b"*" * 1025, lambda x: x)

        for i in (16, 64, 256, 1024):
            value = b"*" * i + b"\x5a" * (1024 - i)
            assert pop(q, None, lambda x: x) == value

        assert pop(q, None, lambda x: x) is None
예제 #42
0
    ###################################

    cages_directory = mkdtemp()

    shared_directory = os_path.join(cages_directory, ".shared")
    mkdir(shared_directory)

    cage_directory = os_path.join(cages_directory, "test")
    mkdir(cage_directory)

    ###################################

    ml = ModuleLocator(cage_directory)

    with expected(InputParameterError):
        ModuleLocator(cage_directory + "notthere")

    ###################################

    with expected(InputParameterError):
        ml.locate("foo.txt")

    with expected(InputParameterError):
        ml.locate("foo..py")

    assert ml.locate("foo.py") is None

    dfn = os_path.join(shared_directory, "foo.py")
    df = open(dfn, "w").close()
    assert ml.locate("foo.py") == dfn
예제 #43
0
    def test_execute_timeout():

        fake_request(1.0)

        with expected(Exception("request deadline waiting for response")):
            pmnc.__getattr__(__name__).execute_reverse("bad_cage", "module", "method", (), {})
예제 #44
0
    print("ok")

    ###################################

    print("module reload timeout: ", end = "")

    fake_request(0.1)
    sleep(0.5)

    write_module(os_path.join("..", ".shared", "reload_timeout.py"),
                 "__all__ = ['foo']\n"
                 "def foo():\n"
                 "    return 1\n"
                 "# EOF")

    with expected(ModuleReloadTimedOutError("request deadline waiting for exclusive access to module reload_timeout")):
        pmnc.reload_timeout.foo()

    fake_request(3.0)

    assert pmnc.reload_timeout.foo() == 1

    print("ok")

    ###################################

    print("__all__ declaration: ", end = "")

    fake_request(30.0)

    write_module(os_path.join("..", ".shared", "all_test.py"),
예제 #45
0
    from expected import expected
    from typecheck import by_regex, InputParameterError
    from threading import Thread

    ###################################

    r = Request()
    assert not r.expired
    assert not r.expires_in(86400.0)
    assert r.elapsed >= 0.0
    assert r.interface is None
    assert r.protocol is None
    assert r.parameters == {}
    assert r.unique_id.startswith("RQ-20")
    assert r._description is None
    with expected(Exception("infinite request never expires")):
        r.expired_for
    assert not r.self_test

    ###################################

    r = Request(timeout = 0.5, interface = "test", protocol = "tcp",
                parameters = { "foo": "bar", "self-test": "some_module" },
                description = "some request")

    assert not r.expired
    assert r.expires_in(1.0)
    assert r.elapsed >= 0.0
    assert r.interface == "test"
    assert r.protocol == "tcp"
    assert r.parameters == { "foo": "bar", "self-test": "some_module" }
예제 #46
0
    from pmnc.request import fake_request, InfiniteRequest

    ###################################

    def wu_skip(): pass

    def wu_loopback(*args, **kwargs):
        return args, kwargs

    ###################################

    print("work unit timeouts: ", end = "")

    wu = WorkUnit(fake_request(0.1), wu_skip, (), {})
    before = time()
    with expected(WorkUnitTimedOut):
        wu.wait()
    after = time()
    assert after - before >= 0.1

    wu = WorkUnit(fake_request(0.1), wu_skip, (), {}); wu()
    before = time()
    wu.wait()
    after = time()
    assert after - before < 0.01

    print("ok")

    ###################################

    print("earliest deadline first ordering: ", end = "")
예제 #47
0
파일: state.py 프로젝트: Tombar/pythomnic3k
 def th_proc():
     fake_request(10.0)
     with expected(ZeroDivisionError):
         pmnc.state.implicit_transaction(txn1)
예제 #48
0
    print("self-testing module interlocked_factory.py:")

    ###################################

    from expected import expected

    ###################################

    class Foo:
        pass

    ilf = InterlockedFactory(Foo)
    assert ilf.count == 0

    with expected(AssertionError):
        ilf.wait(1.0)

    f, n = ilf.create()
    assert isinstance(f, Foo)
    assert n == ilf.count == 1

    with expected(AssertionError):
        ilf.wait(1.0)

    with expected(TypeError):
        ilf.create("will throw")
    assert ilf.count == 1

    ilf.stop()
    with expected(AssertionError):
예제 #49
0
 def _get_guard_fails(func):
     with expected(GuardExpressionException("guarded function foo() requires a \"_when\" "
                                    "argument with guard expression text as its default value")):
         _get_guard(func)
예제 #50
0
    assert _get_guard(foo) == 123

    def foo(a, *, _when): pass
    _get_guard_fails(foo)

    def foo(a, *, _when = "123"): pass
    assert _get_guard(foo) == 123

    def foo(_when, *, a): pass
    _get_guard_fails(foo)

    def foo(_when = "123", *, a): pass
    assert _get_guard(foo) == 123

    def foo(_when = ""): pass
    with expected(GuardExpressionException("invalid guard expression for foo(): "
                                           "unexpected EOF while parsing (foo, line 0)")):
        _get_guard(foo)

    def foo(_when = "@\n"): pass
    with expected(GuardExpressionException("invalid guard expression for foo(): "
                                           "invalid syntax (foo, line 1)")):
        _get_guard(foo)

    ########

    print("ok")

    ############################################################################

    print("simple match args: ", end = "")
예제 #51
0
    sleep(1.25)
    assert t.remain > 0.5
    assert not t.expired
    sleep(1.25)
    assert t.remain == 0.0
    assert t.expired

    t = Timeout(2.0)
    sleep(1.25)
    assert not t.expired
    t.reset()
    assert t.remain > 1.9
    sleep(1.25)
    assert not t.expired

    with expected(InputParameterError("__init__() has got an incompatible value for timeout: 1")):
        Timeout(1)

    before = time()
    t = Timeout(1.0)
    while not t.expired:
        pass
    after = time()
    assert after - before >= 1.0

    ###################################

    t = Timeout(0.5)
    sleep(1.0)
    assert t.expired
    t.reset()
예제 #52
0
    threading.current_thread().name = "MainThread"

    lck = SharedLock("TestLock", None, True)
    assert lck._dump() == "TestLock(ex(-)sh(-))", lck._dump()

    assert lck.acquire()
    assert lck._dump() == "TestLock(ex(MainThread:1)sh(-))", lck._dump()
    lck.release()
    assert lck._dump() == "TestLock(ex(-)sh(-))", lck._dump()

    assert lck.acquire_shared()
    assert lck._dump() == "TestLock(ex(-)sh(MainThread:1))", lck._dump()
    lck.release_shared()
    assert lck._dump() == "TestLock(ex(-)sh(-))", lck._dump()

    with expected(AssertionError("thread MainThread has not acquired the lock")):
        lck.release()

    with expected(AssertionError("thread MainThread has not acquired the lock")):
        lck.release_shared()

    # check that a lock can be acquired in no time

    assert lck.acquire(0.0)
    lck.release()

    assert lck.acquire_shared(0.0)
    lck.release_shared()

    print("ok")