vitess.io/vitess@v0.16.2/go/vt/events/status_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package events
    18  
    19  import (
    20  	"testing"
    21  
    22  	"vitess.io/vitess/go/event"
    23  )
    24  
    25  type testEvent struct {
    26  	StatusUpdater
    27  }
    28  
    29  func TestUpdateInit(t *testing.T) {
    30  	want := "status"
    31  	ev := &testEvent{}
    32  	ev.Update("status")
    33  
    34  	if ev.Status != want {
    35  		t.Errorf("ev.Status = %#v, want %#v", ev.Status, want)
    36  	}
    37  	if ev.EventID == 0 {
    38  		t.Errorf("ev.EventID wasn't initialized")
    39  	}
    40  }
    41  
    42  func TestUpdateEventID(t *testing.T) {
    43  	want := int64(12345)
    44  	ev := &testEvent{}
    45  	ev.EventID = 12345
    46  
    47  	ev.Update("status")
    48  
    49  	if ev.EventID != want {
    50  		t.Errorf("ev.EventID = %v, want %v", ev.EventID, want)
    51  	}
    52  }
    53  
    54  func TestUpdateDispatch(t *testing.T) {
    55  	triggered := false
    56  	event.AddListener(func(ev *testEvent) {
    57  		triggered = true
    58  	})
    59  
    60  	want := "status"
    61  	ev := &testEvent{}
    62  	event.DispatchUpdate(ev, "status")
    63  
    64  	if ev.Status != want {
    65  		t.Errorf("ev.Status = %#v, want %#v", ev.Status, want)
    66  	}
    67  	if !triggered {
    68  		t.Errorf("listener wasn't triggered on Dispatch()")
    69  	}
    70  }