this repo has no description
1defmodule Hobbes.Workloads.LockDatabase do
2 @moduledoc """
3 A test workload that locks and then unlocks the database.
4 """
5
6 alias Hobbes.Transaction
7 alias Hobbes.Structs.Cluster
8
9 alias Trinity.SimProcess
10
11 @behaviour Hobbes.Workloads.Workload
12
13 @type opts :: [
14 delay_ms: non_neg_integer,
15 lock_duration_ms: non_neg_integer,
16 ]
17
18 def run(%{cluster: %Cluster{} = cluster}, opts) do
19 delay_ms = Keyword.get(opts, :delay_ms, 10_000)
20 lock_duration_ms = Keyword.get(opts, :lock_duration_ms, 10_000)
21
22 SimProcess.sleep(delay_ms)
23
24 {:ok, _txn} = do_write(cluster, "\xFF/lock", "true")
25 {:error, {:database_locked, _txn}} = do_write(cluster, "foo", "bar")
26
27 SimProcess.sleep(lock_duration_ms)
28
29 {:ok, _txn} = do_write(cluster, "\xFF/lock", "false")
30 {:ok, _txn} = do_write(cluster, "baz", "foo")
31
32 {:ok, "Locked database"}
33 end
34
35 defp do_write(cluster, key, value) do
36 Transaction.new!(cluster)
37 |> Transaction.write(key, value)
38 |> Transaction.commit()
39 end
40end