Skip to content

Commit c6d6683

Browse files
committed
Add tests for timer and interval with infinity
1 parent db2504e commit c6d6683

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

lib/elixir/lib/stream.ex

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ defmodule Stream do
107107
@type index :: non_neg_integer
108108

109109
@type default :: any
110+
@type timer :: non_neg_integer | :infinity
110111

111112
# Require Stream.Reducers and its callbacks
112113
require Stream.Reducers, as: R
@@ -505,8 +506,10 @@ defmodule Stream do
505506
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
506507
507508
"""
508-
@spec interval(non_neg_integer) :: Enumerable.t()
509-
def interval(n) when is_integer(n) and n >= 0 do
509+
@spec interval(timer()) :: Enumerable.t()
510+
def interval(n)
511+
when is_integer(n) and n >= 0
512+
when n == :infinity do
510513
unfold(0, fn count ->
511514
Process.sleep(n)
512515
{count, count + 1}
@@ -787,8 +790,10 @@ defmodule Stream do
787790
[0]
788791
789792
"""
790-
@spec timer(non_neg_integer) :: Enumerable.t()
791-
def timer(n) when is_integer(n) and n >= 0 do
793+
@spec timer(timer()) :: Enumerable.t()
794+
def timer(n)
795+
when is_integer(n) and n >= 0
796+
when n == :infinity do
792797
take(interval(n), 1)
793798
end
794799

lib/elixir/test/elixir/stream_test.exs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,17 @@ defmodule StreamTest do
499499

500500
test "interval/1" do
501501
stream = Stream.interval(10)
502-
503502
{time_us, value} = :timer.tc(fn -> Enum.take(stream, 5) end)
504503

505504
assert value == [0, 1, 2, 3, 4]
506505
assert time_us >= 50000
507506
end
508507

508+
test "interval/1 with infinity" do
509+
stream = Stream.interval(:infinity)
510+
spawn(Stream, :run, [stream])
511+
end
512+
509513
test "into/2 and run/1" do
510514
Process.put(:stream_cont, [])
511515
Process.put(:stream_done, false)
@@ -1119,6 +1123,11 @@ defmodule StreamTest do
11191123
assert time_us >= 5000
11201124
end
11211125

1126+
test "timer/1 with infinity" do
1127+
stream = Stream.timer(:infinity)
1128+
spawn(Stream, :run, [stream])
1129+
end
1130+
11221131
test "unfold/2" do
11231132
stream = Stream.unfold(10, fn x -> if x > 0, do: {x, x - 1} end)
11241133
assert Enum.take(stream, 5) == [10, 9, 8, 7, 6]

0 commit comments

Comments
 (0)