예제 #1
0
 def test_get_month(self):
     from autopush.db import get_month
     month0 = get_month(0)
     month1 = get_month(1)
     this_month = month0.month
     next_month = 1 if this_month == 12 else this_month + 1
     assert next_month == month1.month
예제 #2
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        conf = AutopushConfig(hostname="example.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf,
                                         resource=autopush.tests.boto_resource)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        db.current_month = last_month.month
        db.message_tables = [
            make_rotating_tablename("message", delta=-1),
            make_rotating_tablename("message", delta=0)
        ]

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        assert next_month.month not in db.message_tables

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert db.current_month == get_month().month
            assert len(db.message_tables) == 2

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
예제 #3
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        settings = AutopushSettings(hostname="example.com",
                                    resolve_hostname=True)

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        settings.current_month = last_month.month
        settings.message_tables = {}

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        settings.message_tables[next_month.month] = None

        # Get the deferred back
        e = Deferred()
        d = settings.update_rotating_tables()

        def check_tables(result):
            eq_(len(settings.message_tables), 2)
            eq_(settings.current_month, get_month().month)

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
예제 #4
0
파일: test_db.py 프로젝트: oremj/autopush
 def test_get_month(self):
     from autopush.db import get_month
     month0 = get_month(0)
     month1 = get_month(1)
     this_month = month0.month
     next_month = 1 if this_month == 12 else this_month + 1
     eq_(next_month, month1.month)
예제 #5
0
    def test_basic_last_connect(self):
        client = yield self.quick_register()
        yield client.disconnect()

        # Verify the last_connect is there and the current month
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))

        # Move it back
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year,
                                            str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Connect/disconnect again and verify last_connect move
        yield client.connect()
        yield client.hello()
        yield client.disconnect()
        times = 0
        while times < 10:
            c = yield deferToThread(self._settings.router.get_uaid,
                                    client.uaid)
            if has_connected_this_month(c):
                break
            else:  # pragma: nocover
                times += 1
                yield client.sleep(1)
        log.debug("Last connected time: %s", c.get("last_connect", "None"))
        eq_(True, has_connected_this_month(c))
예제 #6
0
    def test_update_not_needed(self):
        from autopush.db import get_month
        conf = AutopushConfig(hostname="google.com", resolve_hostname=True)
        db = DatabaseManager.from_config(conf)
        db.create_initial_message_tables()

        # Erase the tables it has on init, and move current month back one
        db.message_tables = {}

        # Create the next month's table, just in case today is the day before
        # a new month, in which case the lack of keys will cause an error in
        # update_rotating_tables
        next_month = get_month(1)
        db.message_tables[next_month.month] = None

        # Get the deferred back
        e = Deferred()
        d = db.update_rotating_tables()

        def check_tables(result):
            assert len(db.message_tables) == 1

        d.addCallback(check_tables)
        d.addBoth(lambda x: e.callback(True))
        return e
예제 #7
0
    def test_basic_last_connect(self):
        client = yield self.quick_register()
        yield client.disconnect()

        # Verify the last_connect is there and the current month
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))

        # Move it back
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year,
                                            str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Connect/disconnect again and verify last_connect move
        yield client.connect()
        yield client.hello()
        yield client.disconnect()
        times = 0
        while times < 10:
            c = yield deferToThread(self._settings.router.get_uaid,
                                    client.uaid)
            if has_connected_this_month(c):
                break
            else:  # pragma: nocover
                times += 1
                yield client.sleep(1)
        log.debug("Last connected time: %s", c.get("last_connect", "None"))
        eq_(True, has_connected_this_month(c))
예제 #8
0
    def test_update_rotating_tables_no_such_table(self, mock_date):
        from autopush.db import get_month
        mock_date.date.today.return_value = get_month(-7)
        settings = AutopushSettings(
            hostname="google.com", resolve_hostname=True)

        # Drop the table created during the creation of autopush settings
        settings.message.table.delete()

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        settings.current_month = last_month.month
        settings.message_tables = {}

        # Get the deferred back
        d = settings.update_rotating_tables()

        def check_tables(result):
            eq_(len(settings.message_tables), 0)

        d.addCallback(check_tables)
        return d
예제 #9
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        settings = AutopushSettings(
            hostname="example.com", resolve_hostname=True)

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        settings.current_month = last_month.month
        settings.message_tables = {}

        # Get the deferred back
        d = settings.update_rotating_tables()

        def check_tables(result):
            eq_(len(settings.message_tables), 1)

        d.addCallback(check_tables)
        return d
예제 #10
0
    def test_update_rotating_tables(self):
        from autopush.db import get_month
        settings = AutopushSettings(
            hostname="example.com", resolve_hostname=True)

        # Erase the tables it has on init, and move current month back one
        last_month = get_month(-1)
        settings.current_month = last_month.month
        settings.message_tables = {}

        # Get the deferred back
        d = settings.update_rotating_tables()

        def check_tables(result):
            eq_(len(settings.message_tables), 1)

        d.addCallback(check_tables)
        return d
예제 #11
0
    def test_basic_last_connect(self):
        client = yield self.quick_register()
        yield client.disconnect()

        # Verify the last_connect is there and the current month
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))

        # Move it back
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year, str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Connect/disconnect again and verify last_connect move
        yield client.connect()
        yield client.hello()
        yield client.disconnect()
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(True, has_connected_this_month(c))
예제 #12
0
    def test_webpush_monthly_rotation(self):
        from autopush.db import make_rotating_tablename

        client = yield self.quick_register(use_webpush=True)
        yield client.disconnect()

        # Move the client back one month to the past
        last_month = make_rotating_tablename(prefix=self._settings._message_prefix, delta=-1)
        lm_message = self._settings.message_tables[last_month]
        yield deferToThread(self._settings.router.update_message_month, client.uaid, last_month)

        # Verify the move
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], last_month)

        # Verify last_connect is current, then move that back
        ok_(has_connected_this_month(c))
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year, str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Move the clients channels back one month
        exists, chans = yield deferToThread(self._settings.message.all_channels, client.uaid)
        eq_(exists, True)
        eq_(len(chans), 1)
        yield deferToThread(lm_message.save_channels, client.uaid, chans)

        # Remove the channels entry entirely from this month
        yield deferToThread(self._settings.message.table.delete_item, uaid=client.uaid, chidmessageid=" ")

        # Verify the channel is gone
        exists, chans = yield deferToThread(self._settings.message.all_channels, client.uaid)
        eq_(exists, False)
        eq_(len(chans), 0)

        # Send in a notification, verify it landed in last months notification
        # table
        data = uuid.uuid4().hex
        yield client.send_notification(data=data)
        notifs = yield deferToThread(lm_message.fetch_messages, client.uaid)
        eq_(len(notifs), 1)

        # Connect the client, verify the migration
        yield client.connect()
        yield client.hello()

        # Pull down the notification
        result = yield client.get_notification()
        chan = client.channels.keys()[0]
        ok_(result is not None)
        eq_(chan, result["channelID"])

        # Check that the client is going to rotate the month
        server_client = self._settings.clients[client.uaid]
        eq_(server_client.ps.rotate_message_table, True)

        # Acknowledge the notification, which triggers the migration
        yield client.ack(chan, result["version"])

        # Wait up to 2 seconds for the table rotation to occur
        start = time.time()
        while time.time() - start < 2:
            c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
            if c["current_month"] == self._settings.current_msg_month:
                break
            else:
                yield deferToThread(time.sleep, 0.2)

        # Verify the month update in the router table
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], self._settings.current_msg_month)
        eq_(server_client.ps.rotate_message_table, False)

        # Verify the client moved last_connect
        eq_(True, has_connected_this_month(c))

        # Verify the channels were moved
        exists, chans = yield deferToThread(self._settings.message.all_channels, client.uaid)
        eq_(exists, True)
        eq_(len(chans), 1)

        yield self.shut_down(client)
예제 #13
0
 def check_tables(result):
     assert db.current_month == get_month().month
     assert len(db.message_tables) == 2
예제 #14
0
 def check_tables(result):
     eq_(len(settings.message_tables), 2)
     eq_(settings.current_month, get_month().month)
예제 #15
0
    def test_webpush_monthly_rotation_prior_record_exists(self):
        from autopush.db import make_rotating_tablename
        client = yield self.quick_register(use_webpush=True)
        yield client.disconnect()

        # Move the client back one month to the past
        last_month = make_rotating_tablename(
            prefix=self._settings._message_prefix, delta=-1)
        lm_message = self._settings.message_tables[last_month]
        yield deferToThread(
            self._settings.router.update_message_month,
            client.uaid,
            last_month
        )

        # Verify the move
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], last_month)

        # Verify last_connect is current, then move that back
        ok_(has_connected_this_month(c))
        today = get_month(delta=-1)
        c["last_connect"] = "%s%s020001" % (today.year,
                                            str(today.month).zfill(2))
        yield deferToThread(c.partial_save)
        eq_(False, has_connected_this_month(c))

        # Move the clients channels back one month
        exists, chans = yield deferToThread(
            self._settings.message.all_channels, client.uaid
        )
        eq_(exists, True)
        eq_(len(chans), 1)
        yield deferToThread(
            lm_message.save_channels,
            client.uaid,
            chans
        )

        # Send in a notification, verify it landed in last months notification
        # table
        data = uuid.uuid4().hex
        yield client.send_notification(data=data)
        notifs = yield deferToThread(lm_message.fetch_messages, client.uaid)
        eq_(len(notifs), 1)

        # Connect the client, verify the migration
        yield client.connect()
        yield client.hello()

        # Pull down the notification
        result = yield client.get_notification()
        chan = client.channels.keys()[0]
        ok_(result is not None)
        eq_(chan, result["channelID"])

        # Check that the client is going to rotate the month
        server_client = self._settings.clients[client.uaid]
        eq_(server_client.ps.rotate_message_table, True)

        # Acknowledge the notification, which triggers the migration
        yield client.ack(chan, result["version"])

        # Wait up to 2 seconds for the table rotation to occur
        start = time.time()
        while time.time()-start < 2:
            c = yield deferToThread(self._settings.router.get_uaid,
                                    client.uaid)
            if c["current_month"] == self._settings.current_msg_month:
                break
            else:
                yield deferToThread(time.sleep, 0.2)

        # Verify the month update in the router table
        c = yield deferToThread(self._settings.router.get_uaid, client.uaid)
        eq_(c["current_month"], self._settings.current_msg_month)
        eq_(server_client.ps.rotate_message_table, False)

        # Verify the client moved last_connect
        eq_(True, has_connected_this_month(c))

        # Verify the channels were moved
        exists, chans = yield deferToThread(
            self._settings.message.all_channels,
            client.uaid
        )
        eq_(exists, True)
        eq_(len(chans), 1)

        yield self.shut_down(client)
예제 #16
0
 def check_tables(result):
     eq_(len(db.message_tables), 2)
     eq_(db.current_month, get_month().month)