github.com/blend/go-sdk@v1.20220411.3/status/freeform_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package status 9 10 import ( 11 "bytes" 12 "context" 13 "fmt" 14 "testing" 15 "time" 16 17 "github.com/blend/go-sdk/assert" 18 "github.com/blend/go-sdk/logger" 19 "github.com/blend/go-sdk/uuid" 20 ) 21 22 func Test_Freeform_CheckStatuses(t *testing.T) { 23 t.Parallel() 24 its := assert.New(t) 25 26 service0 := uuid.V4().String() 27 service1 := uuid.V4().String() 28 29 var calledService0, calledService1 bool 30 ff := NewFreeform() 31 ff.ServiceChecks[service0] = CheckerFunc(func(_ context.Context) error { 32 calledService0 = true 33 return nil 34 }) 35 ff.ServiceChecks[service1] = CheckerFunc(func(_ context.Context) error { 36 calledService1 = true 37 return fmt.Errorf("oh noes") 38 }) 39 40 statuses, err := ff.CheckStatuses(context.Background()) 41 its.Nil(err) 42 its.Len(statuses, 2) 43 its.True(calledService0) 44 its.True(calledService1) 45 its.True(statuses[service0]) 46 its.False(statuses[service1]) 47 } 48 49 func Test_Freeform_CheckStatuses_servicesToCheck(t *testing.T) { 50 t.Parallel() 51 its := assert.New(t) 52 53 service0 := uuid.V4().String() 54 service1 := uuid.V4().String() 55 service2 := uuid.V4().String() 56 57 var calledService0, calledService1, calledService2 bool 58 ff := NewFreeform() 59 ff.ServiceChecks[service0] = CheckerFunc(func(_ context.Context) error { 60 calledService0 = true 61 return nil 62 }) 63 ff.ServiceChecks[service1] = CheckerFunc(func(_ context.Context) error { 64 calledService1 = true 65 return fmt.Errorf("oh noes") 66 }) 67 ff.ServiceChecks[service2] = CheckerFunc(func(_ context.Context) error { 68 calledService2 = true 69 return fmt.Errorf("oh noes") 70 }) 71 72 statuses, err := ff.CheckStatuses(context.Background(), service0, service2) 73 its.Nil(err) 74 its.Len(statuses, 2) 75 its.True(calledService0) 76 its.False(calledService1) 77 its.True(calledService2) 78 its.True(statuses[service0]) 79 its.False(statuses[service2]) 80 } 81 82 func Test_Freeform_CheckStatuses_missing(t *testing.T) { 83 t.Parallel() 84 its := assert.New(t) 85 86 service0 := uuid.V4().String() 87 service1 := uuid.V4().String() 88 service2 := uuid.V4().String() 89 90 var calledService0, calledService1 bool 91 ff := NewFreeform() 92 ff.ServiceChecks[service0] = CheckerFunc(func(_ context.Context) error { 93 calledService0 = true 94 return nil 95 }) 96 ff.ServiceChecks[service1] = CheckerFunc(func(_ context.Context) error { 97 calledService1 = true 98 return fmt.Errorf("oh noes") 99 }) 100 101 statuses, err := ff.CheckStatuses(context.Background(), service1, service2) 102 its.NotNil(err) 103 its.Empty(statuses) 104 105 its.False(calledService0) 106 its.False(calledService1) 107 } 108 109 func Test_Freeform_getCheckStatus(t *testing.T) { 110 t.Parallel() 111 its := assert.New(t) 112 113 service0 := uuid.V4().String() 114 115 var calledService0 bool 116 117 ff := NewFreeform() 118 res := ff.getCheckStatus(context.Background(), service0, CheckerFunc(func(_ context.Context) error { 119 calledService0 = true 120 return nil 121 })) 122 its.True(calledService0) 123 its.True(res.Ok) 124 its.Equal(service0, res.ServiceName) 125 } 126 127 func Test_Freeform_getCheckStatus_logsError(t *testing.T) { 128 t.Parallel() 129 its := assert.New(t) 130 131 logBuffer := new(bytes.Buffer) 132 log := logger.Memory(logBuffer, 133 logger.OptText( 134 logger.OptTextHideTimestamp(), 135 logger.OptTextNoColor(), 136 ), 137 ) 138 defer log.Close() 139 140 service0 := uuid.V4().String() 141 var calledService0 bool 142 ff := NewFreeform( 143 OptFreeformLog(log), 144 ) 145 res := ff.getCheckStatus(context.Background(), service0, CheckerFunc(func(_ context.Context) error { 146 calledService0 = true 147 return fmt.Errorf("this is just a test") 148 })) 149 its.True(calledService0) 150 its.False(res.Ok) 151 its.Equal(service0, res.ServiceName) 152 153 its.Equal("[error] this is just a test\n", logBuffer.String()) 154 } 155 156 func Test_Freeform_getCheckStatus_timeout(t *testing.T) { 157 t.Parallel() 158 its := assert.New(t) 159 160 var calledService bool 161 serviceID := uuid.V4().String() 162 163 called := make(chan struct{}) 164 returned := make(chan struct{}) 165 166 ff := NewFreeform( 167 OptFreeformTimeout(time.Millisecond), 168 ) 169 170 var res freeformCheckResult 171 go func() { 172 defer close(returned) 173 res = ff.getCheckStatus(context.Background(), serviceID, CheckerFunc(func(ctx context.Context) error { 174 calledService = true 175 close(called) 176 <-ctx.Done() 177 return context.Canceled 178 })) 179 }() 180 <-called 181 <-returned 182 its.True(calledService) 183 its.False(res.Ok) 184 its.Equal(serviceID, res.ServiceName) 185 its.Equal(context.Canceled, res.Err) 186 } 187 188 func Test_Freeform_getCheckStatus_panic(t *testing.T) { 189 t.Parallel() 190 its := assert.New(t) 191 192 var calledService bool 193 serviceID := uuid.V4().String() 194 195 ff := NewFreeform( 196 OptFreeformTimeout(time.Millisecond), 197 ) 198 199 res := ff.getCheckStatus(context.Background(), serviceID, CheckerFunc(func(ctx context.Context) error { 200 calledService = true 201 panic("just a test panic!") 202 })) 203 its.True(calledService) 204 its.False(res.Ok) 205 its.Equal(serviceID, res.ServiceName) 206 its.NotNil(res.Err) 207 } 208 209 func Test_Freeform_timeoutOrDefault(t *testing.T) { 210 t.Parallel() 211 its := assert.New(t) 212 213 its.Equal(DefaultFreeformTimeout, NewFreeform().timeoutOrDefault()) 214 its.Equal(DefaultFreeformTimeout<<1, NewFreeform(OptFreeformTimeout(DefaultFreeformTimeout<<1)).timeoutOrDefault()) 215 } 216 217 func Test_Freeform_serviceChecksOrDefault(t *testing.T) { 218 t.Parallel() 219 its := assert.New(t) 220 221 set0 := []string{"alpha", "bravo", "charlie"} 222 set1 := []string{"alpha", "charlie"} 223 set2 := []string{"alpha", uuid.V4().String()} 224 225 ff := NewFreeform() 226 for _, key := range set0 { 227 ff.ServiceChecks[key] = CheckerFunc(func(_ context.Context) error { return nil }) 228 } 229 230 serviceChecks, err := ff.serviceChecksOrDefault() 231 its.Nil(err) 232 its.Len(serviceChecks, 3) 233 its.NotNil(serviceChecks["alpha"]) 234 its.NotNil(serviceChecks["bravo"]) 235 its.NotNil(serviceChecks["charlie"]) 236 237 serviceChecks, err = ff.serviceChecksOrDefault(set1...) 238 its.Nil(err) 239 its.Len(serviceChecks, 2) 240 its.NotNil(serviceChecks["alpha"]) 241 its.Nil(serviceChecks["bravo"]) 242 its.NotNil(serviceChecks["charlie"]) 243 244 serviceChecks, err = ff.serviceChecksOrDefault(set2...) 245 its.NotNil(err) 246 its.Empty(serviceChecks) 247 }