github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgTask/task_test.go (about)

     1  package kmgTask
     2  
     3  import "testing"
     4  
     5  func TestOne(t *testing.T) {
     6  	tm := NewLimitThreadTaskManager(1)
     7  	result_chan := make(chan int)
     8  	var result_var int
     9  	task := TaskFunc(func() {
    10  		result_chan <- 1
    11  		result_var = 2
    12  	})
    13  	tm.AddTask(task)
    14  	tm.AddTaskNewThread(task)
    15  	if <-result_chan != 1 {
    16  		t.Fatalf("not run result_chan not match")
    17  	}
    18  	if <-result_chan != 1 {
    19  		t.Fatalf("not run result_chan not match")
    20  	}
    21  	tm.Close()
    22  	if result_var != 2 {
    23  		t.Fatalf("not run result_var not match")
    24  	}
    25  }
    26  
    27  func TestOneThread(t *testing.T) {
    28  	tm := NewLimitThreadTaskManager(1)
    29  	var result_var int
    30  	result_chan := make(chan int)
    31  	task := TaskFunc(func() {
    32  		result_var = 2
    33  		result_chan <- 1
    34  	})
    35  	tm.AddTask(task)
    36  	tm.AddTask(task)
    37  	for i := 0; i < 2; i++ {
    38  		if <-result_chan != 1 {
    39  			t.Fatalf("result_chan not match")
    40  		}
    41  	}
    42  	tm.Close()
    43  	if result_var != 2 {
    44  		t.Fatalf("result_var not match")
    45  	}
    46  }
    47  
    48  func BenchmarkMulitThread(b *testing.B) {
    49  	tm := NewLimitThreadTaskManager(10)
    50  	tm.AddTask(TaskFunc(func() {
    51  		task := TaskFunc(func() {
    52  		})
    53  		for i := 0; i < b.N; i++ {
    54  			tm.AddTask(task)
    55  		}
    56  	}))
    57  
    58  	tm.Close()
    59  }