github.com/kubeshop/testkube@v1.17.23/pkg/tcl/testworkflowstcl/testworkflowcontroller/watcher_test.go (about)

     1  // Copyright 2024 Testkube.
     2  //
     3  // Licensed as a Testkube Pro file under the Testkube Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //	https://github.com/kubeshop/testkube/blob/main/licenses/TCL.txt
     8  
     9  package testworkflowcontroller
    10  
    11  import (
    12  	"context"
    13  	"sync"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  type test struct {
    21  	value string
    22  }
    23  
    24  func queue(fn func()) {
    25  	var wg sync.WaitGroup
    26  	wg.Add(1)
    27  	go func() {
    28  		wg.Done()
    29  		fn()
    30  	}()
    31  	wg.Wait()
    32  }
    33  
    34  func TestWatcherSync(t *testing.T) {
    35  	w := newWatcher[test](context.Background(), 0)
    36  	defer w.Close()
    37  
    38  	go func() {
    39  		w.SendValue(test{value: "A"})
    40  		w.SendValue(test{value: "B"})
    41  		w.Close()
    42  	}()
    43  	a := <-w.Next(context.Background())
    44  	b := <-w.Next(context.Background())
    45  	c := <-w.Next(context.Background())
    46  	_, ok := <-w.Next(context.Background())
    47  
    48  	assert.Equal(t, WatcherValue[test]{Value: test{value: "A"}}, a)
    49  	assert.Equal(t, WatcherValue[test]{Value: test{value: "B"}}, b)
    50  	assert.Equal(t, WatcherValue[test]{}, c)
    51  	assert.Equal(t, false, ok)
    52  }
    53  
    54  func TestWatcherDistributed(t *testing.T) {
    55  	w := newWatcher[test](context.Background(), 0)
    56  	defer w.Close()
    57  
    58  	queue(func() {
    59  		w.SendValue(test{value: "A"})
    60  		w.SendValue(test{value: "B"})
    61  		w.Close()
    62  	})
    63  
    64  	w.Pause()
    65  	aCh, bCh := w.Next(context.Background()), w.Next(context.Background())
    66  	w.Resume()
    67  	a, b := <-aCh, <-bCh
    68  
    69  	c := <-w.Next(context.Background())
    70  	d := <-w.Next(context.Background())
    71  
    72  	assert.Equal(t, WatcherValue[test]{Value: test{value: "A"}}, a)
    73  	assert.Equal(t, WatcherValue[test]{Value: test{value: "A"}}, b)
    74  	assert.Equal(t, WatcherValue[test]{Value: test{value: "B"}}, c)
    75  	assert.Equal(t, WatcherValue[test]{}, d)
    76  }
    77  
    78  func TestWatcherSyncAdvanced(t *testing.T) {
    79  	w := newWatcher[test](context.Background(), 0)
    80  	defer w.Close()
    81  
    82  	go func() {
    83  		time.Sleep(500 * time.Microsecond)
    84  		w.SendValue(test{value: "A"})
    85  		w.SendValue(test{value: "B"})
    86  		w.Close()
    87  	}()
    88  
    89  	aCh := w.Next(context.Background())
    90  	w.SendValue(test{value: "A"})
    91  	go w.SendValue(test{value: "B"})
    92  	bCh := w.Next(context.Background())
    93  	a, b := <-aCh, <-bCh
    94  
    95  	assert.Equal(t, WatcherValue[test]{Value: test{value: "A"}}, a)
    96  	assert.Equal(t, WatcherValue[test]{Value: test{value: "B"}}, b)
    97  }
    98  
    99  func TestWatcherPause(t *testing.T) {
   100  	w := newWatcher[test](context.Background(), 0)
   101  	defer w.Close()
   102  
   103  	w.Pause()
   104  	aCh := w.Next(context.Background())
   105  	queue(func() {
   106  		w.SendValue(test{value: "A"})
   107  	})
   108  	bCh := w.Next(context.Background())
   109  	time.Sleep(500 * time.Microsecond)
   110  	var a, b WatcherValue[test]
   111  	select {
   112  	case a = <-aCh:
   113  	default:
   114  	}
   115  	select {
   116  	case b = <-bCh:
   117  	default:
   118  	}
   119  
   120  	assert.Equal(t, WatcherValue[test]{}, a)
   121  	assert.Equal(t, WatcherValue[test]{}, b)
   122  }
   123  
   124  func TestWatcherCache(t *testing.T) {
   125  	w := newWatcher[test](context.Background(), 2)
   126  	defer w.Close()
   127  
   128  	a := w.Stream(context.Background())
   129  	queue(func() {
   130  		w.SendValue(test{value: "A"})
   131  		w.SendValue(test{value: "B"})
   132  		w.SendValue(test{value: "C"})
   133  		time.Sleep(500 * time.Microsecond)
   134  		w.SendValue(test{value: "D"})
   135  		w.Close()
   136  	})
   137  	av1 := <-a.Channel()
   138  	av2 := <-a.Channel()
   139  	av3 := <-a.Channel()
   140  	a.Stop()
   141  
   142  	b := w.Stream(context.Background())
   143  	bv1 := <-b.Channel()
   144  	bv2 := <-b.Channel()
   145  	bv3 := <-b.Channel()
   146  	_, ok := <-b.Channel()
   147  
   148  	assert.Equal(t, WatcherValue[test]{Value: test{value: "A"}}, av1)
   149  	assert.Equal(t, WatcherValue[test]{Value: test{value: "B"}}, av2)
   150  	assert.Equal(t, WatcherValue[test]{Value: test{value: "C"}}, av3)
   151  	assert.Equal(t, WatcherValue[test]{Value: test{value: "B"}}, bv1)
   152  	assert.Equal(t, WatcherValue[test]{Value: test{value: "C"}}, bv2)
   153  	assert.Equal(t, WatcherValue[test]{Value: test{value: "D"}}, bv3)
   154  	assert.Equal(t, false, ok)
   155  }