blob: 512014e88eb066988533f8a47e1c15bc23efe0ba [file] [log] [blame]
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -08001package ipc
2
3import (
4 "reflect"
5 "strings"
6 "testing"
7
8 "veyron.io/veyron/veyron2/ipc/version"
9 "veyron.io/veyron/veyron2/naming"
10 "veyron.io/veyron/veyron2/vlog"
11)
12
13func TestIncompatible(t *testing.T) {
14 servers := []string{}
15
16 _, err := filterAndOrderServers(servers, []string{"tcp"})
17 if err == nil || err.Error() != "failed to find any compatible servers: " {
18 t.Errorf("expected a different error: %v", err)
19 }
20
21 for _, a := range []string{"127.0.0.1", "127.0.0.2"} {
22 addr := naming.FormatEndpoint("tcp", a, version.IPCVersionRange{100, 200})
23 name := naming.JoinAddressName(addr, "")
24 servers = append(servers, name)
25 }
26
27 _, err = filterAndOrderServers(servers, []string{"tcp"})
28 if err == nil || (!strings.HasPrefix(err.Error(), "failed to find any compatible servers:") && !strings.Contains(err.Error(), "No compatible IPC versions available")) {
29 vlog.Infof("A: %t . %t", strings.HasPrefix(err.Error(), "failed to find any compatible servers:"), !strings.Contains(err.Error(), "No compatible IPC versions available"))
30 t.Errorf("expected a different error to: %v", err)
31 }
32
33 for _, a := range []string{"127.0.0.3", "127.0.0.4"} {
34 name := naming.JoinAddressName(naming.FormatEndpoint("tcp", a), "")
35 servers = append(servers, name)
36 }
37
38 _, err = filterAndOrderServers(servers, []string{"foobar"})
39 if err == nil || !strings.HasPrefix(err.Error(), "failed to find any servers compatible with [foobar] ") {
40 t.Errorf("expected a different error to: %v", err)
41 }
42
43}
44
45func TestOrderingByProtocol(t *testing.T) {
46 servers := []string{}
47 for _, a := range []string{"127.0.0.3", "127.0.0.4"} {
48 name := naming.JoinAddressName(naming.FormatEndpoint("tcp", a), "")
49 servers = append(servers, name)
50 }
51 for _, a := range []string{"127.0.0.1", "127.0.0.2"} {
52 name := naming.JoinAddressName(naming.FormatEndpoint("tcp4", a), "")
53 servers = append(servers, name)
54 }
55 for _, a := range []string{"127.0.0.10", "127.0.0.11"} {
56 name := naming.JoinAddressName(naming.FormatEndpoint("foobar", a), "")
57 servers = append(servers, name)
58 }
Cosmos Nicolaouba6c68b2014-11-18 22:13:16 -080059 for _, a := range []string{"127.0.0.7", "127.0.0.8"} {
60 name := naming.JoinAddressName(naming.FormatEndpoint("tcp6", a), "")
61 servers = append(servers, name)
62 }
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080063
Cosmos Nicolaouba6c68b2014-11-18 22:13:16 -080064 got, err := filterAndOrderServers(servers, []string{"batman"})
65 if err == nil {
66 t.Fatalf("expected an error")
67 }
68
69 got, err = filterAndOrderServers(servers, []string{"foobar", "tcp4"})
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080070 if err != nil {
71 t.Fatalf("unexpected error: %s", err)
72 }
73
Cosmos Nicolaouba6c68b2014-11-18 22:13:16 -080074 // Just foobar and tcp4
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080075 want := []string{
David Why Use Two When One Will Do Presotto9d3cfb12014-11-20 12:50:54 -080076 "/@3@foobar@127.0.0.10@00000000000000000000000000000000@@@m@@",
77 "/@3@foobar@127.0.0.11@00000000000000000000000000000000@@@m@@",
78 "/@3@tcp4@127.0.0.1@00000000000000000000000000000000@@@m@@",
79 "/@3@tcp4@127.0.0.2@00000000000000000000000000000000@@@m@@",
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080080 }
81 if !reflect.DeepEqual(got, want) {
82 t.Errorf("got: %v, want %v", got, want)
83 }
84
Cosmos Nicolaou28f35c32014-12-01 20:36:27 -080085 // Everything, since we didn't specify a protocol, but ordered by
86 // the internal metric - see defaultPreferredProtocolOrder.
87 // The order will be the default preferred order for protocols, the
88 // original ordering within each protocol, with protocols that
89 // are not in the default ordering list at the end.
90 got, err = filterAndOrderServers(servers, nil)
91 if err != nil {
92 t.Fatalf("unexpected error: %s", err)
93 }
94
95 want = []string{
96 "/@3@tcp4@127.0.0.1@00000000000000000000000000000000@@@m@@",
97 "/@3@tcp4@127.0.0.2@00000000000000000000000000000000@@@m@@",
98 "/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
99 "/@3@tcp@127.0.0.4@00000000000000000000000000000000@@@m@@",
100 "/@3@tcp6@127.0.0.7@00000000000000000000000000000000@@@m@@",
101 "/@3@tcp6@127.0.0.8@00000000000000000000000000000000@@@m@@",
102 "/@3@foobar@127.0.0.10@00000000000000000000000000000000@@@m@@",
103 "/@3@foobar@127.0.0.11@00000000000000000000000000000000@@@m@@",
104 }
105 if !reflect.DeepEqual(got, want) {
106 t.Errorf("got: %v, want %v", got, want)
107 }
108
109 got, err = filterAndOrderServers(servers, []string{})
110 if err != nil {
111 t.Fatalf("unexpected error: %s", err)
112 }
113
114 if !reflect.DeepEqual(got, want) {
115 t.Errorf("got: %v, want %v", got, want)
116 }
117
Cosmos Nicolaouba6c68b2014-11-18 22:13:16 -0800118 got, err = filterAndOrderServers(servers, []string{"tcp"})
119 if err != nil {
120 t.Fatalf("unexpected error: %s", err)
121 }
122 // tcp or tcp4
123 want = []string{
David Why Use Two When One Will Do Presotto9d3cfb12014-11-20 12:50:54 -0800124 "/@3@tcp4@127.0.0.1@00000000000000000000000000000000@@@m@@",
125 "/@3@tcp4@127.0.0.2@00000000000000000000000000000000@@@m@@",
126 "/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
127 "/@3@tcp@127.0.0.4@00000000000000000000000000000000@@@m@@",
Cosmos Nicolaouba6c68b2014-11-18 22:13:16 -0800128 }
129
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -0800130 // Ask for all protocols, with no ordering, except for locality
131 servers = []string{}
132 for _, a := range []string{"74.125.69.139", "127.0.0.3", "127.0.0.1", "192.168.1.10", "74.125.142.83"} {
133 name := naming.JoinAddressName(naming.FormatEndpoint("tcp", a), "")
134 servers = append(servers, name)
135 }
136 for _, a := range []string{"127.0.0.10", "127.0.0.11"} {
137 name := naming.JoinAddressName(naming.FormatEndpoint("foobar", a), "")
138 servers = append(servers, name)
139 }
140 // Everything, since we didn't specify a protocol
141 got, err = filterAndOrderServers(servers, []string{})
142 if err != nil {
143 t.Fatalf("unexpected error: %s", err)
144 }
145 want = []string{
David Why Use Two When One Will Do Presotto9d3cfb12014-11-20 12:50:54 -0800146 "/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
147 "/@3@tcp@127.0.0.1@00000000000000000000000000000000@@@m@@",
148 "/@3@tcp@74.125.69.139@00000000000000000000000000000000@@@m@@",
149 "/@3@tcp@192.168.1.10@00000000000000000000000000000000@@@m@@",
150 "/@3@tcp@74.125.142.83@00000000000000000000000000000000@@@m@@",
151 "/@3@foobar@127.0.0.10@00000000000000000000000000000000@@@m@@",
152 "/@3@foobar@127.0.0.11@00000000000000000000000000000000@@@m@@",
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -0800153 }
154 if !reflect.DeepEqual(got, want) {
155 t.Errorf("got: %v, want %v", got, want)
156 }
157}
158
159func TestOrderByNetwork(t *testing.T) {
160 servers := []string{}
161 for _, a := range []string{"74.125.69.139", "127.0.0.3", "127.0.0.1", "192.168.1.10", "74.125.142.83"} {
162 name := naming.JoinAddressName(naming.FormatEndpoint("tcp", a), "")
163 servers = append(servers, name)
164 }
165 got, err := filterAndOrderServers(servers, []string{"tcp"})
166 if err != nil {
167 t.Fatalf("unexpected error: %s", err)
168 }
169 want := []string{
David Why Use Two When One Will Do Presotto9d3cfb12014-11-20 12:50:54 -0800170 "/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
171 "/@3@tcp@127.0.0.1@00000000000000000000000000000000@@@m@@",
172 "/@3@tcp@74.125.69.139@00000000000000000000000000000000@@@m@@",
173 "/@3@tcp@192.168.1.10@00000000000000000000000000000000@@@m@@",
174 "/@3@tcp@74.125.142.83@00000000000000000000000000000000@@@m@@",
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -0800175 }
176 if !reflect.DeepEqual(got, want) {
177 t.Errorf("got: %v, want %v", got, want)
178 }
179 for _, a := range []string{"74.125.69.139", "127.0.0.3:123", "127.0.0.1", "192.168.1.10", "74.125.142.83"} {
180 name := naming.JoinAddressName(naming.FormatEndpoint("ws", a), "")
181 servers = append(servers, name)
182 }
183
184 got, err = filterAndOrderServers(servers, []string{"ws", "tcp"})
185 if err != nil {
186 t.Fatalf("unexpected error: %s", err)
187 }
188 want = []string{
David Why Use Two When One Will Do Presotto9d3cfb12014-11-20 12:50:54 -0800189 "/@3@ws@127.0.0.3:123@00000000000000000000000000000000@@@m@@",
190 "/@3@ws@127.0.0.1@00000000000000000000000000000000@@@m@@",
191 "/@3@ws@74.125.69.139@00000000000000000000000000000000@@@m@@",
192 "/@3@ws@192.168.1.10@00000000000000000000000000000000@@@m@@",
193 "/@3@ws@74.125.142.83@00000000000000000000000000000000@@@m@@",
194 "/@3@tcp@127.0.0.3@00000000000000000000000000000000@@@m@@",
195 "/@3@tcp@127.0.0.1@00000000000000000000000000000000@@@m@@",
196 "/@3@tcp@74.125.69.139@00000000000000000000000000000000@@@m@@",
197 "/@3@tcp@192.168.1.10@00000000000000000000000000000000@@@m@@",
198 "/@3@tcp@74.125.142.83@00000000000000000000000000000000@@@m@@",
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -0800199 }
200 if !reflect.DeepEqual(got, want) {
201 t.Errorf("got: %v, want %v", got, want)
202 }
203}