github.com/argoproj/argo-cd@v1.8.7/server/application/broadcaster_test.go (about)

     1  package application
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	appv1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
    10  )
    11  
    12  func TestBroadcasterHandler_SubscribeUnsubscribe(t *testing.T) {
    13  	broadcaster := broadcasterHandler{}
    14  
    15  	subscriber := make(chan *appv1.ApplicationWatchEvent)
    16  	unsubscribe := broadcaster.Subscribe(subscriber)
    17  
    18  	assert.Len(t, broadcaster.subscribers, 1)
    19  
    20  	unsubscribe()
    21  	assert.Empty(t, broadcaster.subscribers)
    22  }
    23  
    24  func TestBroadcasterHandler_ReceiveEvents(t *testing.T) {
    25  	broadcaster := broadcasterHandler{}
    26  
    27  	subscriber1 := make(chan *appv1.ApplicationWatchEvent, 1000)
    28  	subscriber2 := make(chan *appv1.ApplicationWatchEvent, 1000)
    29  
    30  	_ = broadcaster.Subscribe(subscriber1)
    31  	_ = broadcaster.Subscribe(subscriber2)
    32  
    33  	firstReceived := false
    34  	secondReceived := false
    35  
    36  	go broadcaster.notify(&appv1.ApplicationWatchEvent{})
    37  
    38  	for {
    39  		select {
    40  		case <-time.After(1 * time.Second):
    41  			t.Error("timeout expired")
    42  			return
    43  		case <-subscriber1:
    44  			firstReceived = true
    45  		case <-subscriber2:
    46  			secondReceived = true
    47  		}
    48  		if firstReceived && secondReceived {
    49  			return
    50  		}
    51  	}
    52  
    53  }