github.com/unionj-cloud/go-doudou@v1.3.8-0.20221011095552-0088008e5b31/framework/registry/delegate_test.go (about) 1 package registry 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "github.com/stretchr/testify/require" 6 "github.com/unionj-cloud/go-doudou/framework/memberlist" 7 "sync" 8 "testing" 9 ) 10 11 func Test_delegate_NodeMeta(t *testing.T) { 12 type fields struct { 13 } 14 type args struct { 15 limit int 16 } 17 tests := []struct { 18 name string 19 fields fields 20 args args 21 want []byte 22 }{ 23 { 24 name: "", 25 fields: fields{}, 26 args: args{ 27 limit: 1024, 28 }, 29 want: nil, 30 }, 31 } 32 for _, tt := range tests { 33 t.Run(tt.name, func(t *testing.T) { 34 d := &delegate{} 35 assert.NotPanics(t, func() { 36 d.NodeMeta(tt.args.limit) 37 }) 38 }) 39 } 40 } 41 func Test_delegate_NodeMeta_panic(t *testing.T) { 42 type fields struct { 43 } 44 type args struct { 45 limit int 46 } 47 tests := []struct { 48 name string 49 fields fields 50 args args 51 want []byte 52 }{ 53 { 54 name: "", 55 fields: fields{}, 56 args: args{ 57 limit: 1, 58 }, 59 want: nil, 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 d := &delegate{ 65 mmeta: mergedMeta{ 66 Meta: nodeMeta{ 67 Service: "test", 68 RouteRootPath: "/api", 69 Port: 6060, 70 Weight: 8, 71 }, 72 Data: map[string]interface{}{ 73 "tags": "service01", 74 }, 75 }, 76 lock: sync.Mutex{}, 77 queue: nil, 78 } 79 assert.Panics(t, func() { 80 d.NodeMeta(tt.args.limit) 81 }) 82 }) 83 } 84 } 85 86 func Test_delegate_NotifyMsg(t *testing.T) { 87 type fields struct { 88 } 89 type args struct { 90 msg []byte 91 } 92 tests := []struct { 93 name string 94 fields fields 95 args args 96 }{ 97 { 98 name: "", 99 fields: fields{}, 100 args: args{ 101 msg: []byte("this is a test msg"), 102 }, 103 }, 104 } 105 for _, tt := range tests { 106 t.Run(tt.name, func(t *testing.T) { 107 d := &delegate{} 108 d.NotifyMsg(tt.args.msg) 109 }) 110 } 111 } 112 113 type testBroadcast struct { 114 node string 115 msg []byte 116 notify chan struct{} 117 } 118 119 func (b *testBroadcast) Invalidates(other memberlist.Broadcast) bool { 120 // Check if that broadcast is a memberlist type 121 mb, ok := other.(*testBroadcast) 122 if !ok { 123 return false 124 } 125 126 // Invalidates any message about the same node 127 return b.node == mb.node 128 } 129 130 // memberlist.NamedBroadcast optional interface 131 func (b *testBroadcast) Name() string { 132 return b.node 133 } 134 135 func (b *testBroadcast) Message() []byte { 136 return b.msg 137 } 138 139 func (b *testBroadcast) Finished() { 140 select { 141 case b.notify <- struct{}{}: 142 default: 143 } 144 } 145 146 func prettyPrintMessages(msgs [][]byte) []string { 147 var out []string 148 for _, msg := range msgs { 149 out = append(out, "'"+string(msg)+"'") 150 } 151 return out 152 } 153 154 func Test_delegate_GetBroadcasts(t *testing.T) { 155 q := &memberlist.TransmitLimitedQueue{RetransmitMult: 3, NumNodes: func() int { return 10 }} 156 157 // 18 bytes per message 158 q.QueueBroadcast(&testBroadcast{"test", []byte("1. this is a test."), nil}) 159 q.QueueBroadcast(&testBroadcast{"foo", []byte("2. this is a test."), nil}) 160 q.QueueBroadcast(&testBroadcast{"bar", []byte("3. this is a test."), nil}) 161 q.QueueBroadcast(&testBroadcast{"baz", []byte("4. this is a test."), nil}) 162 163 // 164 //// 3 byte overhead, should only get 3 messages back 165 //partial := q.GetBroadcasts(3, 80) 166 //require.Equal(t, 3, len(partial), "missing messages: %v", prettyPrintMessages(partial)) 167 168 type fields struct { 169 } 170 type args struct { 171 overhead int 172 limit int 173 } 174 tests := []struct { 175 name string 176 fields fields 177 args args 178 want [][]byte 179 }{ 180 { 181 name: "", 182 fields: fields{}, 183 args: args{ 184 overhead: 2, 185 limit: 80, 186 }, 187 }, 188 } 189 for _, tt := range tests { 190 t.Run(tt.name, func(t *testing.T) { 191 d := &delegate{ 192 queue: q, 193 } 194 got := d.GetBroadcasts(tt.args.overhead, tt.args.limit) 195 require.Equal(t, 4, len(got), "missing messages: %v", prettyPrintMessages(got)) 196 }) 197 } 198 } 199 200 func Test_delegate_LocalState(t *testing.T) { 201 d := delegate{} 202 d.LocalState(false) 203 } 204 205 func Test_delegate_MergeRemoteState(t *testing.T) { 206 d := delegate{} 207 d.MergeRemoteState(nil, false) 208 }