agones.dev/agones@v1.53.0/cmd/ping/udp_test.go (about) 1 // Copyright 2018 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "context" 19 "net" 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 "k8s.io/apimachinery/pkg/util/wait" 25 "k8s.io/utils/clock" 26 testclocks "k8s.io/utils/clock/testing" 27 ) 28 29 type mockAddr struct { 30 addr string 31 } 32 33 func (m *mockAddr) Network() string { 34 return m.addr 35 } 36 37 func (m *mockAddr) String() string { 38 return m.addr 39 } 40 41 func TestUDPServerVisit(t *testing.T) { 42 t.Parallel() 43 44 fc := testclocks.NewFakeClock(time.Now()) 45 u, err := defaultFixture(fc) 46 assert.Nil(t, err) 47 defer u.close() 48 49 // gate 50 assert.Empty(t, u.limits) 51 52 m := &mockAddr{addr: "[::1]:52998"} 53 54 u.rateLimitedEchoResponse([]byte{}, m) 55 56 // gate 57 assert.NotEmpty(t, u.limits) 58 assert.Len(t, u.limits, 1) 59 assert.Equal(t, fc.Now(), u.limits[m.Network()].stamp) 60 61 fc.Step(30 * time.Second) 62 63 u.rateLimitedEchoResponse([]byte{}, m) 64 assert.Len(t, u.limits, 1) 65 assert.Equal(t, fc.Now(), u.limits[m.Network()].stamp) 66 67 m = &mockAddr{addr: "[::1]:52999"} 68 u.rateLimitedEchoResponse([]byte{}, m) 69 assert.Len(t, u.limits, 2) 70 assert.Equal(t, fc.Now(), u.limits[m.Network()].stamp) 71 } 72 73 func TestUDPServerCleanup(t *testing.T) { 74 t.Parallel() 75 76 fc := testclocks.NewFakeClock(time.Now()) 77 u, err := defaultFixture(fc) 78 assert.Nil(t, err) 79 defer u.close() 80 81 // gate 82 assert.Empty(t, u.limits) 83 84 m := &mockAddr{addr: "[::1]:52998"} 85 u.rateLimitedEchoResponse([]byte{}, m) 86 87 // gate 88 assert.NotEmpty(t, u.limits) 89 90 assert.Equal(t, u.clock.Now(), u.limits[m.String()].stamp) 91 fc.Step(10 * time.Second) 92 u.cleanUp() 93 assert.NotEmpty(t, u.limits) 94 95 fc.Step(time.Minute) 96 u.cleanUp() 97 assert.Empty(t, u.limits) 98 } 99 100 func TestUDPServerHealth(t *testing.T) { 101 t.Parallel() 102 103 fc := testclocks.NewFakeClock(time.Now()) 104 u, err := defaultFixture(fc) 105 assert.Nil(t, err) 106 defer u.close() 107 108 assert.Error(t, u.Health()) 109 110 ctx, cancel := context.WithCancel(context.Background()) 111 112 u.run(ctx) 113 assert.NoError(t, u.Health()) 114 115 cancel() 116 117 err = wait.PollUntilContextTimeout(context.Background(), time.Second, 5*time.Second, true, func(_ context.Context) (bool, error) { 118 return u.Health() != nil, nil 119 }) 120 121 assert.Nil(t, err) 122 } 123 124 func defaultFixture(cl clock.WithTickerAndDelayedExecution) (*udpServer, error) { 125 u := newUDPServer(5) 126 u.clock = cl 127 var err error 128 u.conn, err = net.ListenPacket("udp", ":0") 129 return u, err 130 }