View Source Snap.Test (Snap v0.9.0)
Helpers around testing with Snap
.
Unlike SQL databases, ElasticSearch/OpenSearch does not provide transaction
isolation, which is how Ecto.Adapters.SQL.Sandbox
isolates test
processes, allowing multiple tests to run asynchronously.
Snap
uses a different approach. It can set an index namespace for a running
process, which scopes all the convenience API operations to that index
namespace, so they don't collide with each other.
This allows tests to run asynchronously, improving the performance.
To set this up, you'll want to use tags in ExUnit
In your test you'll do something similar to:
setup context do
if context[:snap] do
namespace = Snap.Test.generate_namespace_for_pid(self())
Snap.Cluster.Namespace.set_process_namespace(Cluster, namespace)
Snap.Test.drop_indexes(Cluster)
on_exit(fn ->
Snap.Cluster.Namespace.set_process_namespace(Cluster, namespace)
Snap.Test.drop_indexes(Cluster)
end)
end
end
@tag :snap
test "test something with snap" do
...
end
This generate a unique namespace for the running process and assigns to the cluster. It clears the indexes to make sure the test is starting from scratch.
It setups up a on_exit/1
callback, which runs in a separate process after
the test has completed. The namespace is passed through from the test's
process, so it can teardown all the indexes created by the test.
Summary
Functions
Drops all the indexes on the cluster. Beware!
Generates a String
namespace for the provided pid, by hashing it.
Takes the process namespace from one process and set it on another. Useful if your tests start multiple processes and they all need to have the same view of your cluster.