Skip to content

Commit

Permalink
Merge 86add85 into 6685152
Browse files Browse the repository at this point in the history
  • Loading branch information
predrag-rakic committed Jun 17, 2022
2 parents 6685152 + 86add85 commit daa029c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
19 changes: 10 additions & 9 deletions lib/cloak/vault.ex
Expand Up @@ -181,28 +181,29 @@ defmodule Cloak.Vault do
config = Keyword.merge(app_config, config)

# Start the server
{:ok, pid} = GenServer.start_link(__MODULE__, config, name: __MODULE__)

# Ensure that the configuration is saved
GenServer.call(pid, :save_config, 10_000)

# Return the pid
{:ok, pid}
GenServer.start_link(__MODULE__, config, name: __MODULE__)
end

# Users can override init/1 to customize the configuration
# of the vault during startup
@impl GenServer
def init(config) do
{:ok, config}
# Ensure that the configuration is saved
{:ok, config, {:continue, :save_config}}
end

# Cache the results of the `init` configuration callback in
# the application configuration for this Vault.
@impl GenServer
def handle_call(:save_config, _from, config) do
{:reply, :ok, config, {:continue, :save_config}}
end

@impl GenServer
def handle_continue(:save_config, config) do
Cloak.Vault.save_config(@table_name, config)
{:reply, :ok, config}

{:noreply, config}
end

# If a hot upgrade occurs, rerun the `init` callback to
Expand Down
7 changes: 6 additions & 1 deletion test/cloak/vault_test.exs
Expand Up @@ -49,11 +49,16 @@ defmodule Cloak.VaultTest do
assert SupervisedVault.json_library() == Jason
GenServer.stop(pid)
end

test "can be re-started" do
{:ok, _} = RuntimeVault.start_link()
{:error, {:already_started, _}} = RuntimeVault.start_link()
end
end

describe ".init/1" do
test "returns the given config" do
assert {:ok, []} == TestVault.init([])
assert {:ok, [], {:continue, :save_config}} == TestVault.init([])
end
end

Expand Down

0 comments on commit daa029c

Please sign in to comment.