| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestServerProxyReplaceDrainsPrevious(t *testing.T) { |
| 9 | p := newServerProxy("fs") |
| 10 | // Clients need a name for Host bookkeeping; close is best-effort on nil conn. |
| 11 | a := &Client{name: "fs"} |
| 12 | b := &Client{name: "fs"} |
| 13 | if err := p.replace(context.Background(), a, 1); err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | if p.client() != a { |
| 17 | t.Fatal("active not a") |
| 18 | } |
| 19 | if err := p.replace(context.Background(), b, 2); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | if p.client() != b || p.generation != 2 { |
| 23 | t.Fatal("active not b") |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestHostReplaceServerBackend(t *testing.T) { |
| 28 | h := &Host{} |
| 29 | a := &Client{name: "demo"} |
| 30 | b := &Client{name: "demo"} |
| 31 | if err := h.ReplaceServerBackend(context.Background(), "demo", a, 1); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | if h.client("demo") != a { |
| 35 | t.Fatal("client should be a") |
| 36 | } |
| 37 | if err := h.ReplaceServerBackend(context.Background(), "demo", b, 2); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | if h.client("demo") != b { |
| 41 | t.Fatal("client should be b after replace") |
| 42 | } |
| 43 | h.Close() |
| 44 | if h.client("demo") != nil { |
| 45 | t.Fatal("closed host should not resolve clients") |
| 46 | } |
| 47 | } |
| 48 |