github.com/unirita/cuto@v0.9.8-0.20160830082821-aa6652f877b7/realtime/network/exec_test.go (about) 1 package network 2 3 import ( 4 "regexp" 5 "testing" 6 "time" 7 ) 8 9 func TestNewCommand_WithEmptyRealtimeName(t *testing.T) { 10 cmd := NewCommand("") 11 12 pattern := `^realtime_\d{14}$` 13 matcher := regexp.MustCompile(pattern) 14 if !matcher.MatchString(cmd.GetNetworkName()) { 15 t.Errorf("Unexpected Jobnet name format.") 16 t.Logf("Jobnet name: %s", cmd.GetNetworkName()) 17 } 18 } 19 20 func TestNewCommand_WithRealtimeName(t *testing.T) { 21 cmd := NewCommand("test") 22 23 pattern := `^realtime_test_\d{14}$` 24 matcher := regexp.MustCompile(pattern) 25 if !matcher.MatchString(cmd.GetNetworkName()) { 26 t.Errorf("Unexpected Jobnet name format.") 27 t.Logf("Jobnet name: %s", cmd.GetNetworkName()) 28 } 29 } 30 31 func TestWaitID(t *testing.T) { 32 cmd := new(Command) 33 lineCh := make(chan string, 10) 34 waitCh := make(chan struct{}, 1) 35 idCh := make(chan string, 1) 36 errCh := make(chan string, 1) 37 defer close(lineCh) 38 defer close(waitCh) 39 defer close(idCh) 40 defer close(errCh) 41 42 go cmd.waitID(idCh, errCh, lineCh, waitCh) 43 lineCh <- "testline1" 44 lineCh <- "[network1] STARTED. INSTANCE [12345]" 45 lineCh <- "testline2" 46 47 timer := time.NewTimer(time.Second * 3) 48 select { 49 case id := <-idCh: 50 if id != "12345" { 51 t.Errorf("id => %s, want %s", id, "12345") 52 } 53 case errMsg := <-errCh: 54 t.Errorf("Unexpected err received: %s", errMsg) 55 case <-timer.C: 56 t.Errorf("Test timeout.") 57 } 58 } 59 60 func TestWaitID_ProcessEnd(t *testing.T) { 61 cmd := new(Command) 62 lineCh := make(chan string, 10) 63 waitCh := make(chan struct{}, 1) 64 idCh := make(chan string, 1) 65 errCh := make(chan string, 1) 66 defer close(lineCh) 67 defer close(waitCh) 68 defer close(idCh) 69 defer close(errCh) 70 71 go cmd.waitID(idCh, errCh, lineCh, waitCh) 72 lineCh <- "testline1" 73 lineCh <- "testline2" 74 lineCh <- "testline3" 75 time.Sleep(time.Millisecond * 10) 76 waitCh <- struct{}{} 77 78 timer := time.NewTimer(time.Second * 3) 79 select { 80 case id := <-idCh: 81 t.Errorf("Unexpected id received: %s", id) 82 case errMsg := <-errCh: 83 if errMsg != "testline1\ntestline2\ntestline3\n" { 84 t.Errorf("errMsg => %s, want %s", errMsg, "testline1\ntestline2\ntestline3\n") 85 } 86 case <-timer.C: 87 t.Errorf("Test timeout.") 88 } 89 }