| 78 | } |
| 79 | |
| 80 | func TestAddAndRemoveProxyFromCollection(t *testing.T) { |
| 81 | WithTCPProxy(t, func(conn net.Conn, response chan []byte, proxy *toxiproxy.Proxy) { |
| 82 | collection := toxiproxy.NewProxyCollection() |
| 83 | |
| 84 | if _, err := collection.Get(proxy.Name); err == nil { |
| 85 | t.Error("Expected proxies to be empty") |
| 86 | } |
| 87 | |
| 88 | err := collection.Add(proxy, false) |
| 89 | if err != nil { |
| 90 | t.Error("Expected to be able to add first proxy to collection") |
| 91 | } |
| 92 | |
| 93 | if _, err := collection.Get(proxy.Name); err != nil { |
| 94 | t.Error("Expected proxy to be added to map") |
| 95 | } |
| 96 | |
| 97 | msg := []byte("go away") |
| 98 | |
| 99 | _, err = conn.Write(msg) |
| 100 | if err != nil { |
| 101 | t.Error("Failed writing to socket to shut down server") |
| 102 | } |
| 103 | |
| 104 | conn.Close() |
| 105 | |
| 106 | resp := <-response |
| 107 | if !bytes.Equal(resp, msg) { |
| 108 | t.Error("Server didn't read bytes from client") |
| 109 | } |
| 110 | |
| 111 | err = collection.Remove(proxy.Name) |
| 112 | if err != nil { |
| 113 | t.Error("Expected to remove proxy from collection") |
| 114 | } |
| 115 | |
| 116 | if _, err := collection.Get(proxy.Name); err == nil { |
| 117 | t.Error("Expected proxies to be empty") |
| 118 | } |
| 119 | }) |
| 120 | } |