Esempio n. 1
0
    def test_tracking_and_popping(self):
        """Test adding impressions counts and popping them."""
        counter = Counter()
        utc_now = utctime_ms_reimplement()
        utc_1_hour_after = utc_now + (3600 * 1000)
        counter.track([
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now),
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now),
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now)
        ])

        counter.track([
            Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now),
            Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now)
        ])

        counter.track([
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_1_hour_after),
            Impression('k1', 'f2', 'on', 'l1', 123, None, utc_1_hour_after)
        ])

        assert set(counter.pop_all()) == set([
            Counter.CountPerFeature('f1', truncate_time(utc_now), 3),
            Counter.CountPerFeature('f2', truncate_time(utc_now), 2),
            Counter.CountPerFeature('f1', truncate_time(utc_1_hour_after), 1),
            Counter.CountPerFeature('f2', truncate_time(utc_1_hour_after), 1)
        ])
        assert len(counter._data) == 0
        assert set(counter.pop_all()) == set()
Esempio n. 2
0
    def test_standalone_debug(self, mocker):
        """Test impressions manager in optimized mode with sdk in standalone mode."""

        # Mock utc_time function to be able to play with the clock
        utc_now = truncate_time(utctime_ms_reimplement()) + 1800 * 1000
        utc_time_mock = mocker.Mock()
        utc_time_mock.return_value = utc_now
        mocker.patch('splitio.util.utctime_ms', new=utc_time_mock)

        manager = Manager(ImpressionsMode.DEBUG)  # no listener
        assert manager._counter is None
        assert manager._observer is not None
        assert manager._listener is None

        # An impression that hasn't happened in the last hour (pt = None) should be tracked
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 3), None),
            (Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now - 3), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 3),
            Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now - 3)
        ]

        # Tracking the same impression a ms later should return the impression
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 2), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 2,
                       utc_now - 3)
        ]

        # Tracking a in impression with a different key makes it to the queue
        imps = manager.process_impressions([
            (Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 1), None)
        ])
        assert imps == [
            Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 1)
        ]

        # Advance the perceived clock one hour
        old_utc = utc_now  # save it to compare captured impressions
        utc_now += 3600 * 1000
        utc_time_mock.return_value = utc_now

        # Track the same impressions but "one hour later"
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1), None),
            (Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1,
                       old_utc - 3),
            Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2,
                       old_utc - 1)
        ]

        assert len(
            manager._observer._cache._data) == 3  # distinct impressions seen
Esempio n. 3
0
    def test_non_standalone_optimized(self, mocker):
        """Test impressions manager in optimized mode with sdk in standalone mode."""

        # Mock utc_time function to be able to play with the clock
        utc_now = truncate_time(utctime_ms_reimplement()) + 1800 * 1000
        utc_time_mock = mocker.Mock()
        utc_time_mock.return_value = utc_now
        mocker.patch('splitio.util.utctime_ms', new=utc_time_mock)

        manager = Manager(ImpressionsMode.OPTIMIZED, False)  # no listener
        assert manager._counter is None
        assert manager._observer is None
        assert manager._listener is None

        # An impression that hasn't happened in the last hour (pt = None) should be tracked
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 3), None),
            (Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now - 3), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 3),
            Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now - 3)
        ]

        # Tracking the same impression a ms later should not be empty
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 2), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 2)
        ]

        # Tracking a in impression with a different key makes it to the queue
        imps = manager.process_impressions([
            (Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 1), None)
        ])
        assert imps == [
            Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 1)
        ]

        # Advance the perceived clock one hour
        utc_now += 3600 * 1000
        utc_time_mock.return_value = utc_now

        # Track the same impressions but "one hour later"
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1), None),
            (Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1),
            Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2)
        ]
Esempio n. 4
0
    def test_standalone_optimized_listener(self, mocker):
        """Test impressions manager in optimized mode with sdk in standalone mode."""

        # Mock utc_time function to be able to play with the clock
        utc_now = truncate_time(utctime_ms_reimplement()) + 1800 * 1000
        utc_time_mock = mocker.Mock()
        utc_time_mock.return_value = utc_now
        mocker.patch('splitio.util.utctime_ms', new=utc_time_mock)

        listener = mocker.Mock(spec=ImpressionListenerWrapper)

        manager = Manager(listener=listener)  # no listener
        assert manager._counter is not None
        assert manager._observer is not None
        assert manager._listener is not None

        # An impression that hasn't happened in the last hour (pt = None) should be tracked
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 3), None),
            (Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now - 3), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 3),
            Impression('k1', 'f2', 'on', 'l1', 123, None, utc_now - 3)
        ]

        # Tracking the same impression a ms later should return empty
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 2), None)
        ])
        assert imps == []

        # Tracking a in impression with a different key makes it to the queue
        imps = manager.process_impressions([
            (Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 1), None)
        ])
        assert imps == [
            Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 1)
        ]

        # Advance the perceived clock one hour
        old_utc = utc_now  # save it to compare captured impressions
        utc_now += 3600 * 1000
        utc_time_mock.return_value = utc_now

        # Track the same impressions but "one hour later"
        imps = manager.process_impressions([
            (Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1), None),
            (Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2), None)
        ])
        assert imps == [
            Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1,
                       old_utc - 3),
            Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2,
                       old_utc - 1)
        ]

        assert len(
            manager._observer._cache._data) == 3  # distinct impressions seen
        assert len(
            manager._counter._data
        ) == 3  # 2 distinct features. 1 seen in 2 different timeframes

        assert set(manager._counter.pop_all()) == set([
            Counter.CountPerFeature('f1', truncate_time(old_utc), 3),
            Counter.CountPerFeature('f2', truncate_time(old_utc), 1),
            Counter.CountPerFeature('f1', truncate_time(utc_now), 2)
        ])

        assert listener.log_impression.mock_calls == [
            mocker.call(
                Impression('k1', 'f1', 'on', 'l1', 123, None, old_utc - 3),
                None),
            mocker.call(
                Impression('k1', 'f2', 'on', 'l1', 123, None, old_utc - 3),
                None),
            mocker.call(
                Impression('k1', 'f1', 'on', 'l1', 123, None, old_utc - 2,
                           old_utc - 3), None),
            mocker.call(
                Impression('k2', 'f1', 'on', 'l1', 123, None, old_utc - 1),
                None),
            mocker.call(
                Impression('k1', 'f1', 'on', 'l1', 123, None, utc_now - 1,
                           old_utc - 3), None),
            mocker.call(
                Impression('k2', 'f1', 'on', 'l1', 123, None, utc_now - 2,
                           old_utc - 1), None)
        ]