github.com/kjdelisle/consul@v1.4.5/agent/notify_test.go (about)

     1  package agent
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // Used to be defined in NotifyGroup.WaitCh but was only used in tests and prone
     8  // to leaking memory if anything real did use it because there is no way to
     9  // clear the chan later.
    10  func testWaitCh(t *testing.T, grp *NotifyGroup) chan struct{} {
    11  	ch := make(chan struct{}, 1)
    12  	grp.Wait(ch)
    13  	return ch
    14  }
    15  
    16  func TestNotifyGroup(t *testing.T) {
    17  	grp := &NotifyGroup{}
    18  
    19  	ch1 := testWaitCh(t, grp)
    20  	ch2 := testWaitCh(t, grp)
    21  
    22  	select {
    23  	case <-ch1:
    24  		t.Fatalf("should block")
    25  	default:
    26  	}
    27  	select {
    28  	case <-ch2:
    29  		t.Fatalf("should block")
    30  	default:
    31  	}
    32  
    33  	grp.Notify()
    34  
    35  	select {
    36  	case <-ch1:
    37  	default:
    38  		t.Fatalf("should not block")
    39  	}
    40  	select {
    41  	case <-ch2:
    42  	default:
    43  		t.Fatalf("should not block")
    44  	}
    45  
    46  	// Should be unregistered
    47  	ch3 := testWaitCh(t, grp)
    48  	grp.Notify()
    49  
    50  	select {
    51  	case <-ch1:
    52  		t.Fatalf("should block")
    53  	default:
    54  	}
    55  	select {
    56  	case <-ch2:
    57  		t.Fatalf("should block")
    58  	default:
    59  	}
    60  	select {
    61  	case <-ch3:
    62  	default:
    63  		t.Fatalf("should not block")
    64  	}
    65  }
    66  
    67  func TestNotifyGroup_Clear(t *testing.T) {
    68  	grp := &NotifyGroup{}
    69  
    70  	ch1 := testWaitCh(t, grp)
    71  	grp.Clear(ch1)
    72  
    73  	grp.Notify()
    74  
    75  	// Should not get anything
    76  	select {
    77  	case <-ch1:
    78  		t.Fatalf("should not get message")
    79  	default:
    80  	}
    81  }