Beispiel #1
0
 def setUp(self):
     super(LoxPostgresTests, self).setUp()
     self.config = {
         "backend": {
             "postgres": "postgresql://*****:*****@localhost/postgres"
         }
     }
     self.lox = Lox("everythingbagel", config=self.config)
     self.lox.clear_all()
Beispiel #2
0
def run_test(filename):
    # Run the lox compiler with a file
    lox = Lox()
    # <-- absolute dir the script is in
    script_dir = os.path.dirname(__file__)
    filepath = os.path.join(script_dir, filename)
    print(filepath)
    try:
        with open(filepath, 'r') as f:
            errors = []
            source = f.read()
            lox.run(source, errors)
    except Exception as err:
        print("Error during compilation of {0}. Error is {1}\n{2}".
              format(filepath, str(err), traceback.format_exc()))
Beispiel #3
0
def main(args) -> None:
    if len(args) > 1:
        Lox.usage(64)
    elif len(args) == 1:
        Lox.run_file(args[0])
    else:
        Lox.prompt()
Beispiel #4
0
class LoxRedisTests(LoxTestsBaseMixin, unittest.TestCase):

    def setUp(self):
        self.config = {"backend": {"redis": "redis://:@localhost:6379/0"}}
        self.lox = Lox("poppyseedbagel", config=self.config)
        self.lox.clear_all()

    def tearDown(self):
        self.lox.clear_all()
    
    def test_init(self):
        self.assertEqual(self.lox.name, "poppyseedbagel")
        self.assertEqual(self.lox.config, self.config)
        self.assertEqual(self.lox.locks, {})
        self.assertIsNone(self.lox.context_lock)

    def test_acquire__timeout(self):
        lock = self.lox.acquire(expires_seconds=0.5)
        # make sure the underlying key exists
        self.assertTrue(lock._lock.provider_lock.redis.exists(lock.key))
        # wait long enough for the lock to expire
        time.sleep(0.7)
        # make sure redis removed the lock
        self.assertFalse(lock._lock.provider_lock.redis.exists(lock.key))
Beispiel #5
0
class LoxPostgresTests(LoxTestsBaseMixin, unittest.TestCase):
    def setUp(self):
        super(LoxPostgresTests, self).setUp()
        self.config = {
            "backend": {
                "postgres": "postgresql://*****:*****@localhost/postgres"
            }
        }
        self.lox = Lox("everythingbagel", config=self.config)
        self.lox.clear_all()

    def test_init(self):
        self.assertEqual(self.lox.name, "everythingbagel")
        self.assertEqual(self.lox.config, self.config)
        self.assertEqual(self.lox.locks, {})
        self.assertIsNone(self.lox.context_lock)

    def test_ensure_schema__basic(self):
        # make sure the lox table doesn't exist in the db
        # NOTE this is destructive, we should really run this on a test db
        self.lox.backend.cursor.execute("""
          drop table if exists lox;
        """)

        # create the table
        self.lox.backend.ensure_schema()

        # validate we can directly talk to the lox table in the db

        # first with not null expiration
        test_row = self.__insert_test_row(expire_null=False)
        db_row = self.__select_test_row(test_row, expire_null=False)
        self.assertEqual(db_row, test_row)

        # then with null expiration
        test_row = self.__insert_test_row(expire_null=True)
        db_row = self.__select_test_row(test_row, expire_null=True)
        self.assertEqual(db_row, test_row)

        # don't persist
        self.lox.backend.connection.rollback()

    def test_ensure_schema__wrong_columns(self):
        # make sure the lox table doesn't exist in the db
        # NOTE this is destructive, we should really run this on a test db

        # and create another table with the same name but different schema
        self.lox.backend.cursor.execute("""
          drop table if exists lox;
          create table lox (blah int);
        """)

        # create the table
        with self.assertRaises(SchemaConflictException):
            self.lox.backend.ensure_schema()

    def test_ensure_schema__no_pk(self):
        # make sure the lox table doesn't exist in the db
        # NOTE this is destructive, we should really run this on a test db

        # and create another table with the same name but no primary key
        self.lox.backend.cursor.execute("""
          drop table if exists lox;
          create table lox (
            key text NOT NULL,
            acquire_ts timestamp with time zone NOT NULL,
            expire_ts timestamp with time zone NULL
          );
        """)

        # create the pk
        self.lox.backend.ensure_schema()

        # make sure the pk gets created by trying to insert 2 rows with the same key
        test_row = self.__insert_test_row(expire_null=False)
        db_row = self.__select_test_row(test_row, expire_null=False)
        self.assertEqual(db_row, test_row)

        # 2nd insert should get a PK violation
        with self.assertRaises(psycopg2.IntegrityError):
            self.__do_insert(test_row)

        # don't persist
        self.lox.backend.connection.rollback()

    def __insert_test_row(self, expire_null):
        test_key = "test_{}".format(Lock.generate_id())
        test_ts = datetime.now(tz=pytz.UTC)
        if expire_null:
            test_row = (test_key, test_ts, None)
        else:
            test_row = (test_key, test_ts, test_ts)
        self.__do_insert(test_row)
        return test_row

    def __do_insert(self, test_row):
        self.lox.backend.cursor.execute(
            """
          insert into lox (key, acquire_ts, expire_ts)
          values (%s, %s, %s);
        """, test_row)

    def __select_test_row(self, test_row, expire_null):
        sql = """
          select * from lox
          where key = %s and acquire_ts = %s and expire_ts """
        if expire_null:
            sql += "is %s"
        else:
            sql += "= %s"
        self.lox.backend.cursor.execute(sql, test_row)
        return self.lox.backend.cursor.fetchone()

    def test_acquire__expiration(self):
        lock = self.lox.acquire(expires_seconds=0.5)

    def test_acquire__expiration__check_state(self):
        lock = self.lox.acquire(expires_seconds=1)
        self.assertEqual(lock.state, STATE_ACQUIRED)
        time.sleep(1.5)
        self.assertEqual(lock.state, STATE_EXPIRED)
Beispiel #6
0
 def __ctx_manager(self, test_config):
     with Lox(config=test_config) as test_lox:
         self.assertIsNotNone(test_lox.context_lock)
         self.assertEqual(test_lox.context_lock.state, STATE_ACQUIRED)
     self.assertEqual(test_lox.context_lock.state, STATE_RELEASED)
     return test_lox
Beispiel #7
0
 def setUp(self):
     self.config = {"backend": {"redis": "redis://:@localhost:6379/0"}}
     self.lox = Lox("sesamebagel", config=self.config)
     self.lox.clear_all()
     self.lock = None
     super(LockTests, self).setUp()
Beispiel #8
0
class LockTests(mox.MoxTestBase):

    def setUp(self):
        self.config = {"backend": {"redis": "redis://:@localhost:6379/0"}}
        self.lox = Lox("sesamebagel", config=self.config)
        self.lox.clear_all()
        self.lock = None
        super(LockTests, self).setUp()

    def tearDown(self):
        self.lox.clear_all()
        # clear from the backend manually,
        # since self.lox does not have a reference to this lock
        if self.lock:
            self.lock.clear()
        super(LockTests, self).tearDown()

    def test_init(self):
        self.lock = Lock(self.lox, 1)
        self.assertEqual(self.lock.state, states.STATE_INIT)
        self.assertEqual(self.lock.parent, self.lox)
        self.assertEqual(self.lock.backend, self.lox.backend)
        self.assertEqual(self.lock.config, self.lox.config)
        self.assertEqual(self.lock.id, 1)
        self.assertIsNone(self.lock._lock)

    ## ---- acquire ---- ##

    def test_acquire__with_id(self):
        self.lock = Lock(self.lox, 1)
        self.lock.acquire()
        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)

    def test_acquire__without_id(self):
        self.lock = Lock(self.lox)
        # should assign a UUID automatically
        self.assertIsInstance(self.lock.id, UUID)
        self.lock.acquire()
        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)

    def test_acquire__with_retry(self):
        self.lock = Lock(self.lox)
        self.lock.acquire(retry=True)
        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)

    def test_acquire__with_retry_num_retries(self):
        self.lock = Lock(self.lox)
        self.lock.acquire(retry=True, num_tries=2)
        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)

    def test_acquire__with_retry_num_retries_retry_interval(self):
        self.lock = Lock(self.lox)
        self.lock.acquire(retry=True, num_tries=2, retry_interval_seconds=0.5)
        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)

    def test_acquire__works_on_3rd_try(self):
        self.lock = Lock(self.lox)

        # stub out the back end so we can simulate failures
        self.mox.StubOutWithMock(self.lock.backend, "acquire")

        # two fails, one success
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndRaise(errors.LockInUseException)
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndRaise(errors.LockInUseException)
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndReturn("3rd time's a charm")

        self.mox.ReplayAll()

        self.lock.acquire(retry=True, num_tries=3, retry_interval_seconds=0.1)

        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)

    def test_acquire__fails_multiple_tries(self):
        self.lock = Lock(self.lox)

        # stub out the back end so we can simulate failures
        self.mox.StubOutWithMock(self.lock.backend, "acquire")

        # two fails, one success
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndRaise(errors.LockInUseException)
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndRaise(errors.LockInUseException)
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndRaise(errors.LockInUseException)

        self.mox.ReplayAll()

        with self.assertRaises(errors.LockInUseException):
            self.lock.acquire(retry=True, num_tries=3, retry_interval_seconds=0.1)

        self.assertEqual(self.lock.state, states.STATE_ACQUIRING_TIMEDOUT)

    def test_acquire__fails_no_retries(self):
        self.lock = Lock(self.lox)

        # stub out the back end so we can simulate failures
        self.mox.StubOutWithMock(self.lock.backend, "acquire")

        # two fails, one success
        self.lock.backend.acquire(self.lock.parent.name, self.lock.id, expires_seconds=None).AndRaise(errors.LockInUseException)

        self.mox.ReplayAll()

        with self.assertRaises(errors.LockInUseException):
            self.lock.acquire(retry=False)

        self.assertEqual(self.lock.state, states.STATE_ACQUIRING_EXCEPTION)

    def test_acquire__unexpected_state(self):
        self.lock = Lock(self.lox)
        bad_states = [v for k, v in states.__dict__.iteritems()
                      if k.startswith("STATE_") and v not in states.OK_TO_ACQUIRE]
        for bad_state in bad_states:
            self.lock.state = bad_state
            with self.assertRaises(errors.UnexpectedStateException):
                self.lock.acquire()

    def test_acquire__expiration(self):
        self.lock = Lock(self.lox)
        self.lock.acquire(expires_seconds=1)
        self.assertEqual(self.lock.expires_seconds, 1)

    ## ---- release ---- ##

    def test_release(self):

        self.lock = Lock(self.lox, 1)
        self.lock.acquire()
        self.assertEqual(self.lock.state, states.STATE_ACQUIRED)
        self.lock.release()
        self.assertEqual(self.lock.state, states.STATE_RELEASED)

    def test_release__unexpected_state(self):
        self.lock = Lock(self.lox)
        bad_states = [v for k, v in states.__dict__.iteritems()
                      if k.startswith("STATE_") and v not in states.OK_TO_RELEASE]
        for bad_state in bad_states:
            self.lock.state = bad_state
            with self.assertRaises(errors.UnexpectedStateException):
                self.lock.release()
Beispiel #9
0
 def setUp(self):
     self.config = {"backend": {"redis": "redis://:@localhost:6379/0"}}
     self.lox = Lox("poppyseedbagel", config=self.config)
     self.lox.clear_all()
Beispiel #10
0
 def test_1(self):
     my = Lox(['scan_1.l'])
     assert 10 == len(my.tokens)
Beispiel #11
0
 def test_5(self):
     my = Lox(["scan_5.l"])
     print(len(my.tokens))
     assert 19 == len(my.tokens)
Beispiel #12
0
 def test_3(self):
     my = Lox(["scan_3.l"])
     assert 4 == len(my.tokens)
Beispiel #13
0
 def test_2(self):
     my = Lox(["scan_2.l"])
     print(len(my.tokens))
     assert 17 == len(my.tokens)