Solana.TestValidator (Solana v0.1.0)

View Source

A Solana Test Validator managed by an Elixir process. This allows you to run unit tests as if you had the solana-test-validator tool running in another process.

Requirements

Since Solana.TestValidator uses the solana-test-validator binary, you'll need to have the Solana tool suite installed.

How to Use

You can use the Solana.TestValidator directly or in a supervision tree.

To use it directly, add the following lines to the beginning of your test/test_helper.exs file:

alias Solana.TestValidator
{:ok, validator} = TestValidator.start_link(ledger: "/tmp/test-ledger")
ExUnit.after_suite(fn _ -> TestValidator.stop(validator) end)

This will start and stop the solana-test-validator before and after your tests run.

In a supervision tree

Alternatively, you can add it to your application's supervision tree during tests. Modify your mix.exs file to make the current environment available to your application:

def application do
  [mod: {MyApp, env: Mix.env()}]
end

Then, adjust your application's children depending on the environment:

defmodule MyApp do
  use Application

  def start(_type, env: env) do
    Supervisor.start_link(children(env), strategy: :one_for_one)
  end

  defp children(:test) do
    [
      {Solana.TestValidator, ledger: "/tmp/test_ledger"},
      # ... other children
    ]
  end

  defp children(_) do
    # ...other children
  end
end

Options

You can pass any of the long-form options you would pass to a solana-test-validator here.

For example, to add your own program to the validator, set the bpf_program option as the path to your program's build artifact. See Solana.TestValidator.start_link/1 for more details.

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets the state of a Solana.TestValidator process.

Starts a Solana.TestValidator process linked to the current process.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get_state(validator)

Gets the state of a Solana.TestValidator process.

This is useful when you want to check the latest output of the solana-test-validator.

start_link(config)

Starts a Solana.TestValidator process linked to the current process.

This process runs and monitors a solana-test-validator in the background.

Options

stop(validator)

Stops a Solana.TestValidator process.

Should be called when you want to stop the solana-test-validator.