github.com/unirita/cuto@v0.9.8-0.20160830082821-aa6652f877b7/master/jobnet/path_test.go (about)

     1  // Copyright 2015 unirita Inc.
     2  // Created 2015/04/20 T.Honda
     3  
     4  package jobnet
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  type testJob struct {
    12  	hasError   bool
    13  	isExecuted bool
    14  	Job
    15  }
    16  
    17  func (j *testJob) Execute() (Element, error) {
    18  	j.isExecuted = true
    19  	if j.hasError {
    20  		return nil, fmt.Errorf("job[%s] has error.", j.id)
    21  	}
    22  	return j.Next, nil
    23  }
    24  
    25  func generateTestJob(idnum int) *testJob {
    26  	j := new(testJob)
    27  	j.id = fmt.Sprintf("jobid%d", idnum)
    28  	j.Name = fmt.Sprintf("job%d", idnum)
    29  	return j
    30  }
    31  
    32  func TestNewPath_分岐経路に先頭要素がセットされる(t *testing.T) {
    33  	j1 := generateTestJob(1)
    34  
    35  	p := NewPath(j1)
    36  	if p.Head != j1 {
    37  		t.Errorf("セットされた先頭要素[%v]は想定と違っている", p.Head)
    38  	}
    39  }
    40  
    41  func TestPathRun_ゲートウェイまでのすべてのジョブが実行される_ゲートウェイの後続要素なし(t *testing.T) {
    42  	j1 := generateTestJob(1)
    43  	j2 := generateTestJob(2)
    44  	g1 := NewGateway("gwid1")
    45  
    46  	j1.AddNext(j2)
    47  	j2.AddNext(g1)
    48  	p := NewPath(j1)
    49  
    50  	done := make(chan struct{}, 1)
    51  	p.Run(done)
    52  	if p.Err != nil {
    53  		t.Fatalf("想定外のエラーが発生した: %s", p.Err)
    54  	}
    55  	if p.Goal != g1 {
    56  		t.Fatalf("経路の終着ノード[%v]が想定と違っている。", p.Goal)
    57  	}
    58  	if !j1.isExecuted {
    59  		t.Errorf("job1が実行されなかった。")
    60  	}
    61  	if !j2.isExecuted {
    62  		t.Errorf("job2が実行されなかった。")
    63  	}
    64  }
    65  
    66  func TestPathRun_ゲートウェイまでのすべてのジョブが実行される_ゲートウェイの後続要素あり(t *testing.T) {
    67  	j1 := generateTestJob(1)
    68  	j2 := generateTestJob(2)
    69  	j3 := generateTestJob(3)
    70  	g1 := NewGateway("gwid1")
    71  
    72  	j1.AddNext(j2)
    73  	j2.AddNext(g1)
    74  	g1.AddNext(j3)
    75  	p := NewPath(j1)
    76  
    77  	done := make(chan struct{}, 1)
    78  	p.Run(done)
    79  	if p.Err != nil {
    80  		t.Fatalf("想定外のエラーが発生した: %s", p.Err)
    81  	}
    82  	if p.Goal != g1 {
    83  		t.Fatalf("経路の終着ノード[%v]が想定と違っている。", p.Goal)
    84  	}
    85  	if !j1.isExecuted {
    86  		t.Errorf("job1が実行されなかった。")
    87  	}
    88  	if !j2.isExecuted {
    89  		t.Errorf("job2が実行されなかった。")
    90  	}
    91  	if j3.isExecuted {
    92  		t.Errorf("経路外のジョブjob3が実行された。")
    93  	}
    94  }
    95  
    96  func TestPathRun_ゲートウェイでない終端要素に到達したらエラー(t *testing.T) {
    97  	j1 := generateTestJob(1)
    98  	j2 := generateTestJob(2)
    99  
   100  	j1.AddNext(j2)
   101  	p := NewPath(j1)
   102  
   103  	done := make(chan struct{}, 1)
   104  	p.Run(done)
   105  	if p.Err == nil {
   106  		t.Fatalf("エラーが発生していない。")
   107  	}
   108  	if p.Goal != nil {
   109  		t.Fatalf("ゲートウェイに未到達にも関わらず、終着ノード[%v]がセットされてしまった。", p.Goal)
   110  	}
   111  	if !j1.isExecuted {
   112  		t.Errorf("job1が実行されなかった。")
   113  	}
   114  	if !j2.isExecuted {
   115  		t.Errorf("job2が実行されなかった。")
   116  	}
   117  }
   118  
   119  func TestPathRun_経路の途中でジョブが異常終了したらエラー(t *testing.T) {
   120  	j1 := generateTestJob(1)
   121  	j2 := generateTestJob(2)
   122  	j3 := generateTestJob(3)
   123  	g1 := NewGateway("gwid1")
   124  
   125  	j2.hasError = true
   126  	j1.AddNext(j2)
   127  	j2.AddNext(j3)
   128  	j3.AddNext(g1)
   129  	p := NewPath(j1)
   130  
   131  	done := make(chan struct{}, 1)
   132  	p.Run(done)
   133  	if p.Err == nil {
   134  		t.Fatalf("エラーが発生していない。")
   135  	}
   136  	if p.Goal != nil {
   137  		t.Fatalf("ゲートウェイに未到達にも関わらず、終着ノード[%v]がセットされてしまった。", p.Goal)
   138  	}
   139  	if !j1.isExecuted {
   140  		t.Errorf("job1が実行されなかった。")
   141  	}
   142  	if !j2.isExecuted {
   143  		t.Errorf("job2が実行されなかった。")
   144  	}
   145  	if j3.isExecuted {
   146  		t.Errorf("異常終了ジョブの後続ジョブjob3が実行された。")
   147  	}
   148  }