Browse Source

testing state

master
Rigel Kent 1 year ago
parent
commit
08bd0e1a28
Signed by: rigelk <sendmemail@rigelk.eu> GPG Key ID: EA12971B0E438F36
5 changed files with 83 additions and 32 deletions
  1. +6
    -3
      lib/ambroisie.ex
  2. +2
    -1
      mix.exs
  3. +16
    -11
      priv/runtime.ts
  4. +58
    -16
      test/ambroisie_test.exs
  5. +1
    -1
      test/test_helper.exs

+ 6
- 3
lib/ambroisie.ex View File

@@ -41,6 +41,9 @@ defmodule Ambroisie do
Get a list of all torrents the client manages.
"""
def get(), do: GenServer.call(WebTorrent, :getAll, :infinity)
def getClientState(), do: GenServer.call(WebTorrent, :getClientState, :infinity)
def getClientStats(), do: GenServer.call(WebTorrent, :getClientStats, :infinity)
def getClientOptions(), do: GenServer.call(WebTorrent, :getClientOptions, :infinity)

@doc """
Get a specific entry of the list of torrents managed by the client.
@@ -182,11 +185,11 @@ end
defmodule AmbroisieApp do
use Application

@spec start(any(), any()) :: {:error, any()} | {:ok, pid()}
def start(_type, _args) do
def start(_type, args) do
import Supervisor.Spec, warn: false

ambroisie_opts = %{restore: false}
ambroisie_opts = args

supervisor_opts = [
strategy: :one_for_one,
name: __MODULE__,


+ 2
- 1
mix.exs View File

@@ -48,12 +48,13 @@ defmodule Ambroisie.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
mod: {AmbroisieApp, []}
mod: {AmbroisieApp, %{restore: false}}
]
end

defp aliases do
[
test: "test --no-start",
all: ["deps.get --only #{Mix.env()}", "test"]
]
end


+ 16
- 11
priv/runtime.ts View File

@@ -1,7 +1,7 @@
import { server } from 'node_erlastic'
import webtorrent from 'webtorrent-hybrid'
import { pick, throttle } from 'lodash'
const cfg = require('application-config')('WebTorrent')
const cfg = require('application-config')('Ambroisie/WebTorrent')

interface Torrent {
name: string
@@ -23,7 +23,6 @@ const opts = {
//hash: Function, // custom hash function to use (Function, SHA1 by default),
//krpc: krpc(), // optional k-rpc instance
timeBucketOutdated: 900000, // check buckets every 15min
maxAge: Infinity, // optional setting for announced peers to time out
}, // default = true
webSeeds: true // default = true
}
@@ -49,19 +48,21 @@ client.on('torrent', function saveTorrents () {
// CLIENT HELPERS
function restoreSession (data?) {
if (data.restore && data.state) {
saved = data.state
saved = data.state
} else if (data.restore) {
if (data.path) cfg.filePath = data.path

cfg.read(function (err, data) {
if (err) console.error(err)
saved = data
if (!saved.torrents) saved.torrents = []
saved.torrents.forEach(torrent => {
client.add(torrent.magnetURI)
})
})
}
if (!saved.torrents) saved.torrents = []
saved.torrents.forEach(torrent => {
client.add(torrent.magnetURI, Object.assign({},{
path: torrent.path
}))
})
}

// RETURN HELPERS
@@ -117,12 +118,16 @@ server(
return done('reply', c([t(reduceT(torrent), 'unknown')]))
}

if (term[0] == 'getClientOptions') {
return done('reply', c([t(opts, term[0])]))
if (term == 'getClientOptions') {
return done('reply', opts)
}

if (term == 'getClientStats') {
return done('reply', reduceC(client))
}

if (term[0] == 'getClientStats') {
return done('reply', c(reduceC(client)))
if (term == 'getClientState') {
return done('reply', saved)
}

if (term[0] == 'add') {


+ 58
- 16
test/ambroisie_test.exs View File

@@ -1,29 +1,71 @@
defmodule AmbroisieTest do
defmodule AmbroisieBasicTest do
use ExUnit.Case, async: false
import TimeHelper

setup_all do
AmbroisieApp.start(Ambroisie, [])
:ok
end

@sintel_trailer 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'

test "pool is empty" do
list = Ambroisie.get()[:torrents]
assert [] = list
assert 0 = length(list)
end
describe "should do basic stuff like" do
test "getting the list of torrents running" do
list = Ambroisie.get()[:torrents]
assert [] = list
assert 0 = length(list)
end

test "can add a torrent" do
assert Ambroisie.add(@sintel_trailer) == :ok
end
test "adding a torrent" do
assert Ambroisie.add(@sintel_trailer) == :ok
wait_until(fn -> assert 1 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
end

test "can get a torrent" do
wait_until(fn -> assert 1 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
test "deleting a torrent" do
Ambroisie.remove(@sintel_trailer)
wait_until(fn -> assert 0 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
end
end

test "can delete a torrent" do
Ambroisie.remove(@sintel_trailer)
wait_until(fn -> assert 0 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
describe "should create torrents" do
test "with just the path" do
# Ambroisie.seed()
end
end
end

defmodule AmbroisieStateTest do
use ExUnit.Case, async: false
import TimeHelper

@sintel_trailer %{
name: "Sintel Trailer",
magnetURI: "magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d",
path: "/tmp/webtorrent"
}

describe "should be able to restore state" do
setup do
on_exit fn -> AmbroisieApp.stop([]) end
:ok
end

test "(restore disabled)" do
AmbroisieApp.start(Ambroisie, %{restore: false})
assert 0 = length(Ambroisie.get()[:torrents])
end

test "(restore with a given state)" do
AmbroisieApp.start(Ambroisie, %{restore: true, state: %{torrents: [@sintel_trailer]}})
wait_until(fn -> assert 1 = length(Ambroisie.get()[:torrents]) end)
end

test "can create a torrent" do
# Ambroisie.seed()
test "(restore with the previous state)" do
AmbroisieApp.start(Ambroisie, %{restore: true})
wait_until(fn ->
assert length(Ambroisie.getClientState()[:torrents]) > 0
assert List.first(Ambroisie.getClientState()[:torrents])[:magnetURI] =~ @sintel_trailer[:magnetURI]
end)
end
end
end

+ 1
- 1
test/test_helper.exs View File

@@ -1,4 +1,4 @@
ExUnit.start(seed: 0)
ExUnit.start(seed: 0, trace: true)

defmodule TimeHelper do
def wait_until(fun), do: wait_until(500, fun)


Loading…
Cancel
Save