github.com/weaviate/weaviate@v1.24.6/entities/errors/go_wrapper_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package errors 13 14 import ( 15 "bytes" 16 "os" 17 "sync" 18 "testing" 19 "time" 20 21 "github.com/sirupsen/logrus" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestGoWrapper(t *testing.T) { 26 cases := []struct { 27 env string 28 set bool 29 }{ 30 {env: "something", set: true}, 31 {env: "something", set: false}, 32 {env: "", set: true}, 33 {env: "false", set: true}, 34 // {env: "true", set: true}, // this will NOT recover the panic, but we cannot recover on a higher level and 35 // there is no way to have the test succeed 36 } 37 for _, tt := range cases { 38 t.Run(tt.env, func(t *testing.T) { 39 var buf bytes.Buffer 40 log := logrus.New() 41 log.SetOutput(&buf) 42 43 if tt.set { 44 t.Setenv("DISABLE_RECOVERY_ON_PANIC", tt.env) 45 } 46 wg := sync.WaitGroup{} 47 wg.Add(1) 48 GoWrapper(func() { 49 defer wg.Done() 50 panic("test") 51 }, log) 52 wg.Wait() 53 54 // wait for the recover function in the wrapper to write to the log. This is done after the defer function 55 // in the function we pass to the wrapper has been called and we have no way to block until it is done. 56 // Note that this does not matter in normal operation as we do not depend on the log being written to 57 time.Sleep(100 * time.Millisecond) 58 log.SetOutput(os.Stderr) 59 assert.Contains(t, buf.String(), "Recovered from panic") 60 }) 61 } 62 }