| 27 | } |
| 28 | |
| 29 | func TestNewProtocolSelector(t *testing.T) { |
| 30 | tests := []struct { |
| 31 | name string |
| 32 | protocol string |
| 33 | tunnelTokenProvided bool |
| 34 | expectedProtocol Protocol |
| 35 | hasFallback bool |
| 36 | expectedFallback Protocol |
| 37 | wantErr bool |
| 38 | }{ |
| 39 | { |
| 40 | name: "named tunnel with unknown protocol", |
| 41 | protocol: "unknown", |
| 42 | wantErr: true, |
| 43 | }, |
| 44 | { |
| 45 | name: "named tunnel with h2mux: force to http2", |
| 46 | protocol: "h2mux", |
| 47 | expectedProtocol: HTTP2, |
| 48 | }, |
| 49 | { |
| 50 | name: "named tunnel with http2: no fallback", |
| 51 | protocol: "http2", |
| 52 | expectedProtocol: HTTP2, |
| 53 | }, |
| 54 | { |
| 55 | name: "named tunnel with auto: quic", |
| 56 | protocol: AutoSelectFlag, |
| 57 | expectedProtocol: QUIC, |
| 58 | hasFallback: true, |
| 59 | expectedFallback: HTTP2, |
| 60 | }, |
| 61 | } |
| 62 | |
| 63 | fetcher := dynamicMockFetcher{ |
| 64 | protocolPercents: edgediscovery.ProtocolPercents{}, |
| 65 | } |
| 66 | |
| 67 | for _, test := range tests { |
| 68 | t.Run(test.name, func(t *testing.T) { |
| 69 | selector, err := NewProtocolSelector(test.protocol, testAccountTag, test.tunnelTokenProvided, fetcher.fetch(), ResolveTTL, &log) |
| 70 | if test.wantErr { |
| 71 | assert.Error(t, err, "test %s failed", test.name) |
| 72 | } else { |
| 73 | require.NoError(t, err, "test %s failed", test.name) |
| 74 | assert.Equalf(t, test.expectedProtocol, selector.Current(), "test %s failed", test.name) |
| 75 | fallback, ok := selector.Fallback() |
| 76 | assert.Equalf(t, test.hasFallback, ok, "test %s failed", test.name) |
| 77 | if test.hasFallback { |
| 78 | assert.Equalf(t, test.expectedFallback, fallback, "test %s failed", test.name) |
| 79 | } |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestAutoProtocolSelectorRefresh(t *testing.T) { |
| 86 | fetcher := dynamicMockFetcher{} |