Skip to content

Instantly share code, notes, and snippets.

@AndrewO
Created April 28, 2024 13:45
Show Gist options
  • Save AndrewO/423a49dd95c92b0bf24c64e7dd5e8eac to your computer and use it in GitHub Desktop.
Save AndrewO/423a49dd95c92b0bf24c64e7dd5e8eac to your computer and use it in GitHub Desktop.
Draft of certification test for Elixir Varlink client
defmodule CertificationClient do
@moduledoc """
Varlink client certification tests.
"""
@doc """
Runs the client certification tests.
## Ported from [the Python certification test code](https://github.com/varlink/python/blob/master/varlink/tests/test_certification.py#L31
See [Certification on the Varlink page](https://varlink.org/Language-Bindings#how-to-test-new-language-bindings) for Varlink's recommendations for implementing new libraries.
```python
def run_client(client):
print('Connecting to %s\n' % client)
with client.open('org.varlink.certification') as con:
ret = con.Start()
client_id = ret["client_id"]
print("client_id:", client_id)
ret = con.Test01(client_id)
print("Test01:", ret)
ret = con.Test02(client_id, ret["bool"])
print("Test02:", ret)
ret = con.Test03(client_id, ret["int"])
print("Test03:", ret)
ret = con.Test04(client_id, ret["float"])
print("Test04:", ret)
ret = con.Test05(client_id, ret["string"])
print("Test05:", ret)
ret = con.Test06(client_id, ret["bool"], ret["int"], ret["float"], ret["string"])
print("Test06:", ret)
ret = con.Test07(client_id, ret["struct"])
print("Test07:", ret)
ret = con.Test08(client_id, ret["map"])
print("Test08:", ret)
ret = con.Test09(client_id, ret["set"])
print("Test09:", ret)
ret_array = []
for ret in con.Test10(client_id, ret["mytype"], _more=True):
print("Test10:", ret)
ret_array.append(ret["string"])
ret = con.Test11(client_id, ret_array, _oneway=True)
print("Test11:", ret)
ret = con.End(client_id)
print("End:", ret)
print("Certification passed")
```
"""
alias Varlink.Client
def run(client) do
IO.puts("Connecting to #{client}\n")
# Returns {:ok, interface} or {:error, reason}
{:ok, interface} = Client.open(client, "org.varlink.certification")
# Returns {:ok, ret} or {:error, reason}
{:ok, ret} = Client.send(interface, :Start)
client_id = ret["client_id"]
IO.puts("client_id: #{client_id}")
{:ok, ret} = Client.send(interface, :Test01, client_id)
IO.puts("Test01: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test02, {client_id, ret["bool"]})
IO.puts("Test02: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test03, {client_id, ret["int"]})
IO.puts("Test03: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test04, {client_id, ret["float"]})
IO.puts("Test04: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test05, {client_id, ret["string"]})
IO.puts("Test05: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test06, {client_id, ret["bool"], ret["int"], ret["float"], ret["string"]})
IO.puts("Test06: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test07, {client_id, ret["struct"]})
IO.puts("Test07: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test08, {client_id, ret["map"]})
IO.puts("Test08: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :Test09, {client_id, ret["set"]})
IO.puts("Test09: #{inspect(ret)}")
# Returns a lazy stream that produces more results until the server signals that it is done.
ret_array = Client.send_more(interface, :Test10, {client_id, ret["mytype"]})
|> Stream.map(fn
{:ok, ret, _} -> IO.puts("Test10: #{inspect(ret)}")
ret["string"]
end)
# Returns :ok or {:error, reason}
:ok = Client.send_oneway(interface, :Test11, {client_id, ret_array})
IO.puts("Test11: #{inspect(ret)}")
{:ok, ret} = Client.send(interface, :End, client_id)
IO.puts("End: #{inspect(ret)}")
IO.puts("Certification passed")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment