File tree Expand file tree Collapse file tree 4 files changed +37
-7
lines changed Expand file tree Collapse file tree 4 files changed +37
-7
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,13 @@ defmodule FETestHelpers.HeartbeatTracker do
20
20
end )
21
21
end
22
22
23
+ @ spec count ( tracker ) :: pos_integer
24
+ def count ( pid ) do
25
+ pid
26
+ |> Agent . get ( & & 1 )
27
+ |> Impl . count ( )
28
+ end
29
+
23
30
@ spec list_heartbeat_timestamps ( tracker ) :: [ DateTime . t ( ) ]
24
31
def list_heartbeat_timestamps ( pid ) do
25
32
pid
Original file line number Diff line number Diff line change @@ -19,6 +19,11 @@ defmodule FETestHelpers.HeartbeatTracker.Impl do
19
19
Enum . reverse ( reversed_heartbeats )
20
20
end
21
21
22
+ @ spec count ( t ) :: non_neg_integer
23
+ def count ( reversed_heartbeats ) when is_impl ( reversed_heartbeats ) do
24
+ length ( reversed_heartbeats )
25
+ end
26
+
22
27
@ spec list_heartbeat_intervals ( t , System . time_unit ( ) ) :: [ integer ]
23
28
def list_heartbeat_intervals ( reversed_heartbeats , time_unit \\ :second ) do
24
29
reversed_heartbeats
Original file line number Diff line number Diff line change @@ -13,6 +13,14 @@ defmodule FETestHelpers.HeartbeatTracker.ImplTest do
13
13
assert DateTime . diff ( recorded_timestamp , DateTime . utc_now ( ) , :millisecond ) < 20
14
14
end
15
15
16
+ property "count/1 returns the number of recorded heartbeats" do
17
+ forall heartbeat_count <- non_neg_integer ( ) do
18
+ impl = Impl . new ( ) |> record_heartbeat_multiple_times ( heartbeat_count )
19
+
20
+ heartbeat_count == Impl . count ( impl )
21
+ end
22
+ end
23
+
16
24
property "list_heartbeat_timestamps/1 returns the timestamps in the order received" do
17
25
current_timestamp = DateTime . utc_now ( )
18
26
Original file line number Diff line number Diff line change @@ -4,18 +4,28 @@ defmodule FETestHelpers.HeartbeatTrackerTest do
4
4
5
5
alias FETestHelpers.HeartbeatTracker
6
6
7
- property "record_heartbeat/1 stores the heartbeat " do
8
- forall [ heartbeat_count <- non_neg_integer ( ) ] do
7
+ property "record_heartbeat/1 stores the current timestamp in order each time it's called " do
8
+ forall [ heartbeat_count <- pos_integer ( ) ] do
9
9
{ :ok , tracker } = HeartbeatTracker . start_link ( [ ] )
10
10
11
11
record_heartbeat_multiple_times ( tracker , heartbeat_count )
12
12
13
- actual_heartbeat_count =
14
- tracker
15
- |> HeartbeatTracker . list_heartbeat_timestamps ( )
16
- |> length ( )
13
+ timestamps = HeartbeatTracker . list_heartbeat_timestamps ( tracker )
14
+
15
+ timestamps_sorted? =
16
+ Enum . sort_by ( timestamps , & DateTime . to_unix ( & 1 , :nanosecond ) ) == timestamps
17
+
18
+ timestamps_sorted?
19
+ end
20
+ end
21
+
22
+ property "count/1 returns the number of heartbeats recorded" do
23
+ forall [ heartbeat_count <- non_neg_integer ( ) ] do
24
+ { :ok , tracker } = HeartbeatTracker . start_link ( [ ] )
25
+
26
+ record_heartbeat_multiple_times ( tracker , heartbeat_count )
17
27
18
- actual_heartbeat_count == heartbeat_count
28
+ heartbeat_count == HeartbeatTracker . count ( tracker )
19
29
end
20
30
end
21
31
You can’t perform that action at this time.
0 commit comments