|
@@ -1,11 +1,7 @@
|
|
|
import time
|
|
|
-from datetime import timedelta
|
|
|
-
|
|
|
-from freezegun import freeze_time
|
|
|
|
|
|
from sentry.dynamic_sampling.tasks.task_context import DynamicSamplingLogState, TaskContext, Timers
|
|
|
-
|
|
|
-SECOND = timedelta(seconds=1)
|
|
|
+from sentry.testutils.helpers.datetime import freeze_time
|
|
|
|
|
|
|
|
|
def test_task_context_expiration_time():
|
|
@@ -80,7 +76,7 @@ def test_timer_raw():
|
|
|
t.stop()
|
|
|
|
|
|
# jump 1 second in the future
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
# the timer is stopped so nothing should have happened
|
|
|
assert t.current() == 0
|
|
@@ -96,7 +92,7 @@ def test_timer_raw():
|
|
|
t.start()
|
|
|
|
|
|
# another sec
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
# now the timer should be at 1 sec
|
|
|
assert t.current() == 1.0
|
|
@@ -111,14 +107,14 @@ def test_timer_raw():
|
|
|
t.stop()
|
|
|
assert t.current() == 1.0
|
|
|
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
# check that we can accumulate multiple stops and starts
|
|
|
assert t.current() == 1.0
|
|
|
t.start()
|
|
|
assert t.current() == 1.0
|
|
|
|
|
|
# another sec
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
assert t.current() == 2.0
|
|
|
t.stop()
|
|
@@ -161,7 +157,7 @@ def test_named_timer_raw():
|
|
|
ta.stop()
|
|
|
|
|
|
# jump 1 second in the future
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
# the timer is stopped for a&b so nothing should have happened
|
|
|
assert ta.current() == 0
|
|
@@ -184,7 +180,7 @@ def test_named_timer_raw():
|
|
|
ta.start()
|
|
|
|
|
|
# another sec
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
# now the timer should be at 1 sec
|
|
|
assert ta.current() == 1.0
|
|
@@ -202,7 +198,7 @@ def test_named_timer_raw():
|
|
|
assert tc.current() == 2.0
|
|
|
tb.start()
|
|
|
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
# check that we can accumulate multiple stops and starts
|
|
|
assert ta.current() == 1.0
|
|
|
assert tb.current() == 1.0
|
|
@@ -213,7 +209,7 @@ def test_named_timer_raw():
|
|
|
assert tc.current() == 3.0
|
|
|
|
|
|
# another sec
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
assert ta.current() == 2.0
|
|
|
assert tb.current() == 2.0
|
|
@@ -224,7 +220,7 @@ def test_named_timer_raw():
|
|
|
assert tc.current() == 4.0
|
|
|
|
|
|
# another sec
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
assert ta.current() == 2.0
|
|
|
assert tb.current() == 3.0
|
|
@@ -243,8 +239,8 @@ def test_timer_context_manager():
|
|
|
assert t.current("a") == i
|
|
|
|
|
|
# jump one sec
|
|
|
- frozen_time.tick()
|
|
|
- frozen_time.tick()
|
|
|
+ frozen_time.shift(1)
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
# we advanced 3 seconds within the timer and 3 outside we should have only counted 3
|
|
|
assert t.current("a") == 3
|
|
@@ -265,21 +261,21 @@ def test_named_timer_context_manager():
|
|
|
|
|
|
with t.get_timer("a") as ta:
|
|
|
assert ta.current() == i
|
|
|
- frozen_time.tick(delta=SECOND)
|
|
|
+ frozen_time.shift(1)
|
|
|
with t.get_timer("b") as tb:
|
|
|
assert tb.current() == i * 2
|
|
|
- frozen_time.tick(delta=SECOND * 2)
|
|
|
+ frozen_time.shift(2)
|
|
|
with t.get_timer("c") as tc:
|
|
|
assert tc.current() == i * 3
|
|
|
- frozen_time.tick(delta=SECOND * 3)
|
|
|
+ frozen_time.shift(3)
|
|
|
|
|
|
# jump one sec
|
|
|
- frozen_time.tick(delta=SECOND)
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
- frozen_time.tick(delta=SECOND)
|
|
|
+ frozen_time.shift(1)
|
|
|
|
|
|
# outside the context manager timers should not advance
|
|
|
- frozen_time.tick(delta=SECOND * 100)
|
|
|
+ frozen_time.shift(100)
|
|
|
|
|
|
assert t.current("a") == 3
|
|
|
assert t.current("b") == 3 * 2
|
|
@@ -292,10 +288,10 @@ def test_task_context_serialisation():
|
|
|
with freeze_time("2023-07-12 10:00:00") as frozen_time:
|
|
|
# a timer without state
|
|
|
with task.get_timer("a"):
|
|
|
- frozen_time.tick(delta=SECOND)
|
|
|
+ frozen_time.shift(1)
|
|
|
# a timer with state
|
|
|
with task.get_timer("b"):
|
|
|
- frozen_time.tick(delta=SECOND * 2)
|
|
|
+ frozen_time.shift(2)
|
|
|
state = task.get_function_state("b")
|
|
|
state.num_iterations = 1
|
|
|
state.num_orgs = 2
|