github.com/blend/go-sdk@v1.20220411.3/async/worker_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 async
     9  
    10  import (
    11  	"context"
    12  	"sync"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestWorker(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	var didWork bool
    22  	wg := sync.WaitGroup{}
    23  	wg.Add(1)
    24  	w := NewWorker(func(_ context.Context, obj interface{}) error {
    25  		defer wg.Done()
    26  		didWork = true
    27  		assert.Equal("hello", obj)
    28  		return nil
    29  	})
    30  	go func() { _ = w.Start() }()
    31  	<-w.NotifyStarted()
    32  
    33  	assert.True(w.Latch.IsStarted())
    34  	w.Enqueue("hello")
    35  	wg.Wait()
    36  	assert.Nil(w.Stop())
    37  
    38  	assert.False(w.Latch.IsStarted())
    39  	assert.True(didWork)
    40  }