github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/kv/thread_safe_map_test.go (about) 1 package kv_test 2 3 import ( 4 "bytes" 5 "github.com/benz9527/toy-box/algo/kv" 6 "github.com/stretchr/testify/assert" 7 "io" 8 "testing" 9 "time" 10 11 "github.com/onsi/ginkgo/v2" 12 "github.com/onsi/ginkgo/v2/types" 13 "github.com/onsi/gomega" 14 ) 15 16 func TestZeroCopyFile(t *testing.T) { 17 gomega.RegisterFailHandler(ginkgo.Fail) 18 ginkgo.RunSpecs(t, "Thread Safe Map Suite", 19 types.SuiteConfig{ 20 LabelFilter: "ThreadSafeMap", 21 ParallelTotal: 1, 22 ParallelProcess: 1, 23 GracePeriod: 5 * time.Second, 24 }, 25 types.ReporterConfig{ 26 Verbose: true, 27 }, 28 ) 29 } 30 31 type testCloser struct { 32 closeByErr bool 33 id string 34 io.Writer 35 } 36 37 func (t *testCloser) Close() error { 38 _, _ = t.Writer.Write(bytes.NewBufferString(t.id + " closed\n").Bytes()) 39 if t.closeByErr { 40 return io.ErrUnexpectedEOF 41 } 42 return nil 43 } 44 45 var _ = ginkgo.Describe("Thread Safe Map Element io.Closer", 46 ginkgo.Label("ThreadSafeMap"), 47 func() { 48 ginkgo.It("should close all elements", func() { 49 m := kv.NewThreadSafeMap[*testCloser]() 50 m.AddOrUpdate("1", &testCloser{id: "1", Writer: ginkgo.GinkgoWriter}) 51 m.AddOrUpdate("2", &testCloser{id: "2", closeByErr: true, Writer: ginkgo.GinkgoWriter}) 52 err := m.Close() 53 assert.NoError(ginkgo.GinkgoT(), err) 54 }) 55 }, 56 )