...

Source file src/github.com/emissary-ingress/emissary/v3/cmd/entrypoint/testutil_fake_notifier_test_test.go

Documentation: github.com/emissary-ingress/emissary/v3/cmd/entrypoint

     1  package entrypoint_test
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/emissary-ingress/emissary/v3/cmd/entrypoint"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestFakeNotifier(t *testing.T) {
    14  	n := entrypoint.NewNotifier()
    15  
    16  	// These will track invocations of our two test listeners. One test listener will be created
    17  	// early, prior to any changes, the other will be created later after changes have occurred.
    18  	earlyCh := make(chan string)
    19  	lateCh := make(chan string)
    20  
    21  	stopEarly := n.Listen(generator("early", earlyCh))
    22  
    23  	// No listener should be notified until we signal it.
    24  	require.Equal(t, "", get(earlyCh))
    25  	require.Equal(t, "", get(lateCh))
    26  	n.Changed()
    27  	require.Equal(t, "", get(earlyCh))
    28  	require.Equal(t, "", get(lateCh))
    29  
    30  	// Send notifications and check that we saw one for the early listener.
    31  	n.Notify()
    32  	require.Equal(t, "early-1", get(earlyCh))
    33  	require.Equal(t, "", get(lateCh))
    34  
    35  	// Create the late listener and verify that it "catches up".
    36  	stopLate := n.Listen(generator("late", lateCh))
    37  	require.Equal(t, "", get(earlyCh))
    38  	require.Equal(t, "late-1", get(lateCh))
    39  
    40  	// Check that multiple changes will get batched.
    41  	n.Changed()
    42  	n.Changed()
    43  
    44  	n.Notify()
    45  	require.Equal(t, "early-2", get(earlyCh))
    46  	require.Equal(t, "late-2", get(lateCh))
    47  
    48  	// No changes have happened.
    49  	n.Notify()
    50  	require.Equal(t, "", get(earlyCh))
    51  	require.Equal(t, "", get(lateCh))
    52  
    53  	// Both should get notified.
    54  	n.Changed()
    55  	n.Notify()
    56  	require.Equal(t, "early-3", get(earlyCh))
    57  	require.Equal(t, "late-3", get(lateCh))
    58  
    59  	// Check auto notification.
    60  	n.AutoNotify(true)
    61  	n.Changed()
    62  	require.Equal(t, "early-4", get(earlyCh))
    63  	require.Equal(t, "late-4", get(lateCh))
    64  
    65  	// Check going back to manual notify.
    66  	n.AutoNotify(false)
    67  	n.Changed()
    68  	require.Equal(t, "", get(earlyCh))
    69  	require.Equal(t, "", get(lateCh))
    70  	n.Notify()
    71  	require.Equal(t, "early-5", get(earlyCh))
    72  	require.Equal(t, "late-5", get(lateCh))
    73  
    74  	// Now let's stop and see that there are no notifications.
    75  	stopEarly()
    76  	stopLate()
    77  	n.Changed()
    78  	n.Notify()
    79  	require.Equal(t, "", get(earlyCh))
    80  	require.Equal(t, "", get(lateCh))
    81  }
    82  
    83  func generator(name string, ch chan string) func() {
    84  	count := 0
    85  	return func() {
    86  		count += 1
    87  		go func() {
    88  			ch <- fmt.Sprintf("%s-%d", name, count)
    89  		}()
    90  	}
    91  }
    92  
    93  var timeout time.Duration
    94  
    95  func init() {
    96  	// Use a longer timeout in CI to avoid flakes.
    97  	if os.Getenv("CI") != "" {
    98  		timeout = 1 * time.Second
    99  	} else {
   100  		timeout = 100 * time.Millisecond
   101  	}
   102  }
   103  
   104  func get(ch chan string) string {
   105  	select {
   106  	case result := <-ch:
   107  		return result
   108  	case <-time.After(timeout):
   109  		return ""
   110  	}
   111  }
   112  

View as plain text