go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/async/worker_test.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package async 9 10 import ( 11 "context" 12 "sync" 13 "testing" 14 15 . "go.charczuk.com/sdk/assert" 16 ) 17 18 func TestWorker(t *testing.T) { 19 var didWork bool 20 wg := sync.WaitGroup{} 21 wg.Add(1) 22 var gotObj interface{} 23 w := NewWorker(func(_ context.Context, obj interface{}) error { 24 defer wg.Done() 25 didWork = true 26 gotObj = obj 27 return nil 28 }) 29 go func() { _ = w.Start() }() 30 <-w.NotifyStarted() 31 32 ItsEqual(t, true, w.Latch.IsStarted()) 33 w.Work <- "hello" 34 wg.Wait() 35 ItsEqual(t, "hello", gotObj) 36 ItsNil(t, w.Stop()) 37 38 ItsEqual(t, false, w.Latch.IsStarted()) 39 ItsEqual(t, true, didWork) 40 }