Initial Commit

This commit is contained in:
2024-12-24 17:45:52 +01:00
commit c30845575a
42 changed files with 870 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
defmodule KV.BucketTest do
use ExUnit.Case, async: true
setup do
bucket = start_supervised!(KV.Bucket)
%{bucket: bucket}
end
test "stores values by key", %{bucket: bucket} do
assert KV.Bucket.get(bucket, "milk") == nil
KV.Bucket.put(bucket, "milk", 3)
assert KV.Bucket.get(bucket, "milk") == 3
end
end

View File

@@ -0,0 +1,42 @@
defmodule KV.RegistryTest do
use ExUnit.Case, async: true
setup context do
_ = start_supervised!({KV.Registry, name: context.test})
%{registry: context.test}
end
test "spawns buckets", %{registry: registry} do
assert KV.Registry.lookup(registry, "shopping") == :error
KV.Registry.create(registry, "shopping")
assert {:ok, bucket} = KV.Registry.lookup(registry, "shopping")
KV.Bucket.put(bucket, "milk", 1)
assert KV.Bucket.get(bucket, "milk") == 1
end
test "removes buckets on exit", %{registry: registry} do
KV.Registry.create(registry, "shopping")
{:ok, bucket} = KV.Registry.lookup(registry, "shopping")
Agent.stop(bucket)
# Do a call to ensure the registry processed the DOWN message
_ = KV.Registry.create(registry, "bogus")
assert KV.Registry.lookup(registry, "shopping") == :error
end
test "removes bucket on crash", %{registry: registry} do
KV.Registry.create(registry, "shopping")
{:ok, bucket} = KV.Registry.lookup(registry, "shopping")
# Stop the bucket with non-normal reason
# If a process terminates with a reason other than :normal, all linked processes
# receive an EXIT signal, causing the linked process to
# also terminate unless it is trapping exits.
Agent.stop(bucket, :shutdown)
# Do a call to ensure the registry processed the DOWN message
_ = KV.Registry.create(registry, "bogus")
assert KV.Registry.lookup(registry, "shopping") == :error
end
end

1
kv/test/test_helper.exs Normal file
View File

@@ -0,0 +1 @@
ExUnit.start()