github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/waitlivemsg/waitlivemsg_test.go (about)

     1  package waitlivemsg
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/henvic/uilive"
     8  )
     9  
    10  func TestWaitLiveMsg(t *testing.T) {
    11  	var us = uilive.New()
    12  	var wlm = WaitLiveMsg{}
    13  	wlm.SetStream(us)
    14  	var one = NewMessage("foo")
    15  	var two = NewMessage("bar")
    16  
    17  	wlm.AddMessage(one)
    18  	wlm.AddMessage(two)
    19  	go wlm.Wait()
    20  	time.Sleep(2 * time.Second)
    21  	one.PlayText("xyz")
    22  	time.Sleep(3 * time.Second)
    23  	wlm.SetMessage(two)
    24  	time.Sleep(3 * time.Second)
    25  	var three = NewMessage("iziiz three")
    26  	wlm.AddMessage(three)
    27  	time.Sleep(2 * time.Second)
    28  	three.StopText("iziiz stopped")
    29  	time.Sleep(2 * time.Second)
    30  	three.PlayText("iziiz continued")
    31  	time.Sleep(2 * time.Second)
    32  	wlm.RemoveMessage(three)
    33  	time.Sleep(2 * time.Second)
    34  	wlm.Stop()
    35  }
    36  
    37  func TestWaitLiveMsgStopNoTick(t *testing.T) {
    38  	var wlm = WaitLiveMsg{}
    39  
    40  	end := make(chan struct{}, 1)
    41  	timer := time.NewTimer(time.Second)
    42  
    43  	go func() {
    44  		wlm.Stop()
    45  		end <- struct{}{}
    46  	}()
    47  
    48  	select {
    49  	case <-timer.C:
    50  		t.Errorf("WaitLiveMsg didn't stop before tick")
    51  	case <-end:
    52  	}
    53  }