github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/servant/servant_test.go (about) 1 package main 2 3 import ( 4 "os" 5 "syscall" 6 "testing" 7 "time" 8 9 "github.com/unirita/cuto/servant/config" 10 "github.com/unirita/cuto/servant/remote" 11 "github.com/unirita/cuto/testutil" 12 ) 13 14 func testEventLoop(signalCh <-chan os.Signal, sq <-chan *remote.Session) { 15 16 } 17 18 func waitEventLoopEnd(sigCh <-chan os.Signal, sq <-chan *remote.Session, endCh chan<- struct{}) { 19 eventLoop(sigCh, sq) 20 endCh <- struct{}{} 21 } 22 23 func TestRun_メッセージ受信開始に失敗したらエラー(t *testing.T) { 24 config.Servant = config.DefaultServantConfig() 25 config.Servant.Sys.BindPort = 65536 26 _, err := Run() 27 if err == nil { 28 t.Error("エラーが発生していない。") 29 } 30 } 31 32 func TestRun_メッセージ受信開始に成功(t *testing.T) { 33 eventLoopFunc = testEventLoop 34 config.Servant = config.DefaultServantConfig() 35 config.Servant.Sys.BindPort = 65531 36 r, err := Run() 37 if err != nil { 38 t.Error("エラーが発生した - %v", err) 39 } 40 if r != 0 { 41 t.Errorf("不正な戻り値 : %v", r) 42 } 43 } 44 45 func TestEventLoop_SIGINTシグナルを受信するとループが終了する(t *testing.T) { 46 isTest = true 47 48 sigCh := make(chan os.Signal) 49 sq := make(chan *remote.Session) 50 endCh := make(chan struct{}) 51 go waitEventLoopEnd(sigCh, sq, endCh) 52 53 sigCh <- syscall.SIGINT 54 select { 55 case <-endCh: 56 // 問題なし 57 case <-time.After(time.Millisecond * 100): 58 t.Errorf("ループが終了しない。") 59 } 60 } 61 62 func TestEventLoop_ハングアップしてもループが終了しない(t *testing.T) { 63 isTest = true 64 65 sigCh := make(chan os.Signal) 66 sq := make(chan *remote.Session) 67 endCh := make(chan struct{}) 68 go waitEventLoopEnd(sigCh, sq, endCh) 69 70 sigCh <- syscall.SIGHUP 71 select { 72 case <-endCh: 73 t.Errorf("ループが終了した。") 74 case <-time.After(time.Millisecond * 100): 75 sigCh <- syscall.SIGINT 76 } 77 } 78 79 func TestEventLoop_セッションキューに挿入したセッションが実行される(t *testing.T) { 80 isTest = true 81 82 // あえてエラーが発生するSession.Bodyをセットし、doJobRequestまでロジックを進ませない 83 body := `{ 84 "type":"request", 85 "nid":1234, 86 "jid":"001", 87 "path":"test.bat", 88 "param":"$SSJOBNET:ID$", 89 "env":"testenv=val", 90 "workspace": "", 91 "warnrc":4, 92 "warnstr":"warn", 93 "errrc":12, 94 "errstr":"error" 95 }` 96 97 conn := testutil.NewConnStub() 98 session := remote.NewSession(conn, body) 99 100 sigCh := make(chan os.Signal) 101 sq := make(chan *remote.Session) 102 go eventLoop(sigCh, sq) 103 104 sq <- session 105 106 // Session実行ごルーチンの終了待ちのためにwait 107 time.Sleep(100 * time.Millisecond) 108 if conn.WriteStr == "" { 109 t.Error("セッションが実行されていない。") 110 } 111 }