github.com/git-amp/amp-sdk-go@v0.7.5/stdlib/task/pool_worker_test.go (about)

     1  package task_test
     2  
     3  import (
     4  	"sync"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/git-amp/amp-sdk-go/stdlib/task"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestPoolWorker(t *testing.T) {
    13  	t.Run("disallows simultaneous processing of items with the same UniqueID", func(t *testing.T) {
    14  		t.Parallel()
    15  
    16  		var wg sync.WaitGroup
    17  		item := makeItems()
    18  		item[1].block = make(chan struct{})
    19  		item[1].retry = true
    20  
    21  		w, err := task.StartNewPoolWorker("", 2, task.NewStaticScheduler(100*time.Millisecond, 2*time.Second))
    22  
    23  		require.NoError(t, err)
    24  		defer w.Close()
    25  
    26  		w.Add(item[1])
    27  		w.Add(item[2])
    28  
    29  		var which *workItem
    30  		wg.Add(1)
    31  		go func() {
    32  			select {
    33  			case <-item[1].processed:
    34  				which = item[1]
    35  			case <-item[2].processed:
    36  				which = item[2]
    37  			}
    38  
    39  			select {
    40  			case <-item[1].processed:
    41  				t.Fatalf("nope")
    42  			case <-item[2].processed:
    43  				t.Fatalf("nope")
    44  			case <-time.After(1 * time.Second):
    45  			}
    46  			wg.Done()
    47  		}()
    48  		wg.Wait()
    49  
    50  		close(which.block)
    51  		wg.Add(1)
    52  		go func() {
    53  			select {
    54  			case <-item[1].processed:
    55  			case <-item[2].processed:
    56  			}
    57  
    58  			select {
    59  			case <-item[1].processed:
    60  				t.Fatalf("nope")
    61  			case <-item[2].processed:
    62  				t.Fatalf("nope")
    63  			case <-time.After(1 * time.Second):
    64  			}
    65  			wg.Done()
    66  		}()
    67  		wg.Wait()
    68  	})
    69  }