-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
668 additions
and
569 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
defmodule BufferTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :buffer | ||
test "buffer" do | ||
testproc = self() | ||
|
||
start = 1 | ||
tend = 100 | ||
size = 3 | ||
|
||
# Create a range. | ||
Obs.range(start, tend, 100) | ||
|> Obs.buffer(size) | ||
|> Obs.each(fn v -> | ||
Logger.debug("Sending #{inspect(v)}") | ||
send(testproc, v) | ||
end) | ||
|
||
# Receive all the values. | ||
Enum.chunk_every(1..100, size) | ||
|> Enum.map(fn list -> | ||
Logger.debug("Waiting for #{inspect(list)}") | ||
|
||
receive do | ||
^list -> Logger.debug("Got list #{inspect(list)}") | ||
end | ||
end) | ||
|
||
# Receive no other values. | ||
receive do | ||
x -> | ||
Logger.error("Received another value, did not want") | ||
assert "received another value:" == "" | ||
after | ||
1000 -> | ||
:ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
defmodule ChunkTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :chunk | ||
test "chunk" do | ||
testproc = self() | ||
|
||
start = 1 | ||
tend = 50 | ||
size = 5 | ||
|
||
# Create a range. | ||
Obs.range(start, tend, 100) | ||
# We should consume 5 values each time. | ||
|> Obs.chunk(size * 100 + 10) | ||
|> Obs.each(fn v -> | ||
Logger.debug("Sending #{inspect(v, charlists: :as_lists)}") | ||
send(testproc, v) | ||
end) | ||
|
||
# Receive all the values. | ||
Enum.chunk_every(1..50, size) | ||
|> Enum.map(fn list -> | ||
Logger.debug("Waiting for #{inspect(list, charlists: :as_lists)}") | ||
|
||
receive do | ||
^list -> Logger.debug("Got list #{inspect(list, charlists: :as_lists)}") | ||
after | ||
10000 -> | ||
assert "Did not receive list in time: #{inspect(list, charlists: :as_lists)}" == "" | ||
end | ||
end) | ||
|
||
# Receive no other values. | ||
receive do | ||
x -> | ||
Logger.error("Received another value, did not want") | ||
assert "received another value: #{inspect(x, charlists: :as_lists)} " == "" | ||
after | ||
1000 -> | ||
:ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
defmodule CombineLatestSilentTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :combinelatestsilent | ||
test "Combine Latest Silent" do | ||
# 1 2 3 | ||
# 11 12 13 14 15 | ||
# =================================== | ||
# 1/11 2/11 3/12 | ||
|
||
testproc = self() | ||
|
||
{:ok, pid1} = GenObservable.spawn_supervised(Observables.Subject) | ||
xs = Obs.from_pid(pid1) | ||
|
||
{:ok, pid2} = GenObservable.spawn_supervised(Observables.Subject) | ||
ys = Obs.from_pid(pid2) | ||
|
||
Obs.combinelatestsilent(xs, ys, left: nil, right: nil, silent: :right) | ||
|> Obs.inspect() | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
# Send first value, should not produce. | ||
GenObservable.send_event(pid1, :x0) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
100 -> :ok | ||
end | ||
|
||
# Send second value, should not produce because silent. | ||
GenObservable.send_event(pid2, :y0) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
100 -> :ok | ||
end | ||
|
||
# Update the left observable. Should produce with history. | ||
GenObservable.send_event(pid1, :x1) | ||
assert_receive({:x1, :y0}, 5000, "did not get this message {:x1, :y0}!") | ||
|
||
# Update the right observable, should be silent. | ||
GenObservable.send_event(pid2, :y1) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
100 -> :ok | ||
end | ||
|
||
GenObservable.send_event(pid2, :y2) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
100 -> :ok | ||
end | ||
|
||
GenObservable.send_event(pid2, :y3) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
100 -> :ok | ||
end | ||
|
||
# Send a final value, should produce. | ||
GenObservable.send_event(pid1, :x2) | ||
assert_receive({:x2, :y3}, 1000, "did not get this message {:x2, :y3}!") | ||
|
||
# Mailbox should be empty. | ||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
100 -> :ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule CombineLatestTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :combinelatest | ||
test "Combine Latest" do | ||
testproc = self() | ||
|
||
{:ok, pid1} = GenObservable.spawn_supervised(Observables.Subject) | ||
xs = Obs.from_pid(pid1) | ||
|
||
{:ok, pid2} = GenObservable.spawn_supervised(Observables.Subject) | ||
ys = Obs.from_pid(pid2) | ||
|
||
Obs.combinelatest(xs, ys, left: nil, right: nil) | ||
|> Obs.inspect() | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
# Send first value, should not produce. | ||
GenObservable.send_event(pid1, :x0) | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
0 -> :ok | ||
end | ||
|
||
# Send second value, should produce. | ||
GenObservable.send_event(pid2, :y0) | ||
assert_receive({:x0, :y0}, 1000, "did not get this message!") | ||
|
||
# Update the left observable. Shoudl produce with history. | ||
GenObservable.send_event(pid1, :x1) | ||
assert_receive({:x1, :y0}, 1000, "did not get this message!") | ||
|
||
GenObservable.send_event(pid2, :y1) | ||
assert_receive({:x1, :y1}, 1000, "did not get this message!") | ||
|
||
# Send a final value, should produce. | ||
GenObservable.send_event(pid1, :xfinal) | ||
assert_receive({:xfinal, :y1}, 1000, "did not get this message!") | ||
|
||
receive do | ||
x -> flunk("Mailbox was supposed to be empty, got: #{inspect(x)}") | ||
after | ||
0 -> :ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule DistinctTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :distinct | ||
test "Distinct" do | ||
testproc = self() | ||
|
||
xs = [1, 1, 1, 1] | ||
|
||
xs | ||
|> Obs.from_enum(100) | ||
|> Obs.distinct() | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
Enum.uniq(xs) | ||
|> Enum.map(fn x -> | ||
receive do | ||
^x -> :ok | ||
end | ||
|
||
receive do | ||
^x -> assert "duplicates" == "" | ||
after | ||
1000 -> :ok | ||
end | ||
end) | ||
|
||
assert 5 == 5 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule EachTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :each | ||
test "Each" do | ||
testproc = self() | ||
|
||
xs = [1, 1, 1, 1] | ||
|
||
xs | ||
|> Obs.from_enum(100) | ||
|> Obs.each(fn x -> IO.inspect(x) end) | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
xs | ||
|> Enum.map(fn x -> | ||
receive do | ||
^x -> :ok | ||
end | ||
end) | ||
|
||
receive do | ||
x -> | ||
Logger.error("Received another value, did not want") | ||
assert 5 == 10 | ||
after | ||
1000 -> | ||
:ok | ||
end | ||
|
||
assert 5 == 5 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule FilterTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
@tag :filter | ||
test "Filter" do | ||
testproc = self() | ||
|
||
xs = [1, 2, 3, 1, 2, 3, 3, 2, 1] | ||
|
||
xs | ||
|> Obs.from_enum(100) | ||
|> Obs.filter(fn x -> x > 2 end) | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
xs | ||
|> Enum.filter(fn x -> x > 2 end) | ||
|> Enum.map(fn x -> | ||
receive do | ||
^x -> :ok | ||
end | ||
end) | ||
|
||
receive do | ||
x -> | ||
Logger.error("Received another value, did not want") | ||
assert "received another value: #{inspect(x)}" == "" | ||
after | ||
1000 -> | ||
:ok | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule FromEnumTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :fromenum | ||
test "Test from enum" do | ||
testproc = self() | ||
|
||
xs = [1, 2, 3] | ||
|
||
xs | ||
|> Obs.from_enum(1000) | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
xs | ||
|> Enum.map(fn x -> | ||
receive do | ||
^x -> :ok | ||
end | ||
end) | ||
|
||
assert 5 == 5 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule MapTest do | ||
use ExUnit.Case | ||
alias Observables.{Obs, GenObservable} | ||
require Logger | ||
|
||
@tag :map | ||
test "Map" do | ||
testproc = self() | ||
|
||
xs = [1, 1, 1, 1] | ||
|
||
xs | ||
|> Obs.from_enum(100) | ||
|> Obs.map(fn x -> x + 100 end) | ||
|> Obs.map(fn v -> send(testproc, v) end) | ||
|
||
Enum.map(xs, fn x -> x + 100 end) | ||
|> Enum.map(fn x -> | ||
receive do | ||
^x -> :ok | ||
end | ||
end) | ||
|
||
assert 5 == 5 | ||
end | ||
end |
Oops, something went wrong.