Browse Source

testing state

Rigel Kent 1 year ago
parent
commit
08bd0e1a28
Signed by: Rigel Kent <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
41 41
   Get a list of all torrents the client manages.
42 42
   """
43 43
   def get(), do: GenServer.call(WebTorrent, :getAll, :infinity)
44
+  def getClientState(), do: GenServer.call(WebTorrent, :getClientState, :infinity)
45
+  def getClientStats(), do: GenServer.call(WebTorrent, :getClientStats, :infinity)
46
+  def getClientOptions(), do: GenServer.call(WebTorrent, :getClientOptions, :infinity)
44 47
 
45 48
   @doc """
46 49
   Get a specific entry of the list of torrents managed by the client.
@@ -182,11 +185,11 @@ end
182 185
 defmodule AmbroisieApp do
183 186
   use Application
184 187
 
185
-  @spec start(any(), any()) :: {:error, any()} | {:ok, pid()}
186
-  def start(_type, _args) do
188
+  def start(_type, args) do
187 189
     import Supervisor.Spec, warn: false
188 190
 
189
-    ambroisie_opts = %{restore: false}
191
+    ambroisie_opts = args
192
+
190 193
     supervisor_opts = [
191 194
       strategy: :one_for_one,
192 195
       name: __MODULE__,

+ 2
- 1
mix.exs View File

@@ -48,12 +48,13 @@ defmodule Ambroisie.MixProject do
48 48
   # Run "mix help compile.app" to learn about applications.
49 49
   def application do
50 50
     [
51
-      mod: {AmbroisieApp, []}
51
+      mod: {AmbroisieApp, %{restore: false}}
52 52
     ]
53 53
   end
54 54
 
55 55
   defp aliases do
56 56
     [
57
+      test: "test --no-start",
57 58
       all: ["deps.get --only #{Mix.env()}", "test"]
58 59
     ]
59 60
   end

+ 16
- 11
priv/runtime.ts View File

@@ -1,7 +1,7 @@
1 1
 import { server } from 'node_erlastic'
2 2
 import webtorrent from 'webtorrent-hybrid'
3 3
 import { pick, throttle } from 'lodash'
4
-const cfg = require('application-config')('WebTorrent')
4
+const cfg = require('application-config')('Ambroisie/WebTorrent')
5 5
 
6 6
 interface Torrent {
7 7
   name: string
@@ -23,7 +23,6 @@ const opts = {
23 23
     //hash: Function,  // custom hash function to use (Function, SHA1 by default),
24 24
     //krpc: krpc(),     // optional k-rpc instance
25 25
     timeBucketOutdated: 900000, // check buckets every 15min
26
-    maxAge: Infinity,  // optional setting for announced peers to time out
27 26
   }, // default = true
28 27
   webSeeds: true // default = true
29 28
 }
@@ -49,19 +48,21 @@ client.on('torrent', function saveTorrents () {
49 48
 // CLIENT HELPERS
50 49
 function restoreSession (data?) {
51 50
   if (data.restore && data.state) {
52
-    saved = data.state 
51
+    saved = data.state
53 52
   } else if (data.restore) {
54 53
     if (data.path) cfg.filePath = data.path
55 54
 
56 55
     cfg.read(function (err, data) {
57 56
       if (err) console.error(err)
58 57
       saved = data
59
-      if (!saved.torrents) saved.torrents = []
60
-      saved.torrents.forEach(torrent => {
61
-        client.add(torrent.magnetURI)
62
-      })
63 58
     })
64 59
   }
60
+  if (!saved.torrents) saved.torrents = []
61
+  saved.torrents.forEach(torrent => {
62
+    client.add(torrent.magnetURI, Object.assign({},{
63
+      path: torrent.path
64
+    }))
65
+  })
65 66
 }
66 67
 
67 68
 // RETURN HELPERS
@@ -117,12 +118,16 @@ server(
117 118
       return done('reply', c([t(reduceT(torrent), 'unknown')]))
118 119
     }
119 120
 
120
-    if (term[0] == 'getClientOptions') {
121
-      return done('reply', c([t(opts, term[0])]))
121
+    if (term == 'getClientOptions') {
122
+      return done('reply', opts)
123
+    }
124
+
125
+    if (term == 'getClientStats') {
126
+      return done('reply', reduceC(client))
122 127
     }
123 128
 
124
-    if (term[0] == 'getClientStats') {
125
-      return done('reply', c(reduceC(client)))
129
+    if (term == 'getClientState') {
130
+      return done('reply', saved)
126 131
     }
127 132
 
128 133
     if (term[0] == 'add') {

+ 58
- 16
test/ambroisie_test.exs View File

@@ -1,29 +1,71 @@
1
-defmodule AmbroisieTest do
1
+defmodule AmbroisieBasicTest do
2 2
   use ExUnit.Case, async: false
3 3
   import TimeHelper
4 4
 
5
+  setup_all do
6
+    AmbroisieApp.start(Ambroisie, [])
7
+    :ok
8
+  end
9
+
5 10
   @sintel_trailer 'magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d'
6 11
 
7
-  test "pool is empty" do
8
-    list = Ambroisie.get()[:torrents]
9
-    assert [] = list
10
-    assert 0 = length(list)
11
-  end
12
+  describe "should do basic stuff like" do
13
+    test "getting the list of torrents running" do
14
+      list = Ambroisie.get()[:torrents]
15
+      assert [] = list
16
+      assert 0 = length(list)
17
+    end
12 18
 
13
-  test "can add a torrent" do
14
-    assert Ambroisie.add(@sintel_trailer) == :ok
15
-  end
19
+    test "adding a torrent" do
20
+      assert Ambroisie.add(@sintel_trailer) == :ok
21
+      wait_until(fn -> assert 1 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
22
+    end
16 23
 
17
-  test "can get a torrent" do
18
-    wait_until(fn -> assert 1 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
24
+    test "deleting a torrent" do
25
+      Ambroisie.remove(@sintel_trailer)
26
+      wait_until(fn -> assert 0 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
27
+    end
19 28
   end
20 29
 
21
-  test "can delete a torrent" do
22
-    Ambroisie.remove(@sintel_trailer)
23
-    wait_until(fn -> assert 0 = length(Ambroisie.get(@sintel_trailer)[:torrents]) end)
30
+  describe "should create torrents" do
31
+    test "with just the path" do
32
+      # Ambroisie.seed()
33
+    end
24 34
   end
35
+end
36
+
37
+defmodule AmbroisieStateTest do
38
+  use ExUnit.Case, async: false
39
+  import TimeHelper
40
+
41
+  @sintel_trailer %{
42
+    name: "Sintel Trailer",
43
+    magnetURI: "magnet:?xt=urn:btih:6a9759bffd5c0af65319979fb7832189f4f3c35d",
44
+    path: "/tmp/webtorrent"
45
+  }
46
+
47
+  describe "should be able to restore state" do
48
+    setup do
49
+      on_exit fn -> AmbroisieApp.stop([]) end
50
+      :ok
51
+    end
52
+
53
+    test "(restore disabled)" do
54
+      AmbroisieApp.start(Ambroisie, %{restore: false})
55
+      assert 0 = length(Ambroisie.get()[:torrents])
56
+    end
57
+
58
+    test "(restore with a given state)" do
59
+      AmbroisieApp.start(Ambroisie, %{restore: true, state: %{torrents: [@sintel_trailer]}})
60
+      wait_until(fn -> assert 1 = length(Ambroisie.get()[:torrents]) end)
61
+    end
25 62
 
26
-  test "can create a torrent" do
27
-    # Ambroisie.seed()
63
+    test "(restore with the previous state)" do
64
+      AmbroisieApp.start(Ambroisie, %{restore: true})
65
+      wait_until(fn ->
66
+        assert length(Ambroisie.getClientState()[:torrents]) > 0
67
+        assert List.first(Ambroisie.getClientState()[:torrents])[:magnetURI] =~ @sintel_trailer[:magnetURI]
68
+      end)
69
+    end
28 70
   end
29 71
 end

+ 1
- 1
test/test_helper.exs View File

@@ -1,4 +1,4 @@
1
-ExUnit.start(seed: 0)
1
+ExUnit.start(seed: 0, trace: true)
2 2
 
3 3
 defmodule TimeHelper do
4 4
   def wait_until(fun), do: wait_until(500, fun)

Loading…
Cancel
Save