github.com/leanovate/gopter@v0.2.9/runner_test.go (about)

     1  package gopter
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestRunnerSingleWorker(t *testing.T) {
    11  	parameters := DefaultTestParameters()
    12  	testRunner := &runner{
    13  		parameters: parameters,
    14  		worker: func(num int, shouldStop shouldStop) *TestResult {
    15  			return &TestResult{
    16  				Status:    TestPassed,
    17  				Succeeded: 1,
    18  				Discarded: 0,
    19  			}
    20  		},
    21  	}
    22  
    23  	result := testRunner.runWorkers()
    24  
    25  	if result.Status != TestPassed ||
    26  		result.Succeeded != 1 ||
    27  		result.Discarded != 0 {
    28  		t.Errorf("Invalid result: %#v", result)
    29  	}
    30  }
    31  
    32  func TestRunnerParallelWorkers(t *testing.T) {
    33  	parameters := DefaultTestParameters()
    34  	specs := []struct {
    35  		workers int
    36  		res     []TestResult
    37  		exp     *TestResult
    38  		wait    []int
    39  	}{
    40  		// Test all pass
    41  		{
    42  			workers: 50,
    43  			res: []TestResult{
    44  				{
    45  					Status:    TestPassed,
    46  					Succeeded: 10,
    47  					Discarded: 1,
    48  				},
    49  			},
    50  			exp: &TestResult{
    51  				Status:    TestPassed,
    52  				Succeeded: 500,
    53  				Discarded: 50,
    54  			},
    55  		},
    56  		// Test exhausted
    57  		{
    58  			workers: 50,
    59  			res: []TestResult{
    60  				{
    61  					Status:    TestExhausted,
    62  					Succeeded: 1,
    63  					Discarded: 10,
    64  				},
    65  			},
    66  			exp: &TestResult{
    67  				Status:    TestExhausted,
    68  				Succeeded: 50,
    69  				Discarded: 500,
    70  			},
    71  		},
    72  		// Test all fail
    73  		{
    74  			workers: 50,
    75  			res: []TestResult{
    76  				{
    77  					Status:    TestFailed,
    78  					Succeeded: 0,
    79  					Discarded: 0,
    80  					Labels:    []string{"some label"},
    81  					Error:     errors.New("invalid result 0 != 1"),
    82  				},
    83  			},
    84  			exp: &TestResult{
    85  				Status:    TestFailed,
    86  				Succeeded: 0,
    87  				Discarded: 0,
    88  				Labels:    []string{"some label"},
    89  				Error:     errors.New("invalid result 0 != 1"),
    90  			},
    91  		},
    92  		// a pass and failure
    93  		{
    94  			workers: 2,
    95  			res: []TestResult{
    96  				{
    97  					Status:    TestPassed,
    98  					Succeeded: 94,
    99  					Discarded: 1,
   100  				},
   101  				{
   102  					Status:    TestFailed,
   103  					Succeeded: 4,
   104  					Discarded: 3,
   105  					Labels:    []string{"some label"},
   106  					Error:     errors.New("invalid result 0 != 2"),
   107  				},
   108  			},
   109  			exp: &TestResult{
   110  				Status:    TestFailed,
   111  				Succeeded: 98,
   112  				Discarded: 4,
   113  				Labels:    []string{"some label"},
   114  				Error:     errors.New("invalid result 0 != 2"),
   115  			},
   116  			wait: []int{1, 0},
   117  		},
   118  		// a pass and multiple failures (first failure returned)
   119  		{
   120  			workers: 3,
   121  			res: []TestResult{
   122  				{
   123  					Status:    TestPassed,
   124  					Succeeded: 94,
   125  					Discarded: 1,
   126  				},
   127  				{
   128  					Status:    TestFailed,
   129  					Succeeded: 3,
   130  					Discarded: 2,
   131  					Labels:    []string{"worker 1"},
   132  					Error:     errors.New("worker 1 error"),
   133  				},
   134  				{
   135  					Status:    TestFailed,
   136  					Succeeded: 1,
   137  					Discarded: 1,
   138  					Labels:    []string{"worker 2"},
   139  					Error:     errors.New("worker 2 error"),
   140  				},
   141  			},
   142  			exp: &TestResult{
   143  				Status:    TestFailed,
   144  				Succeeded: 98,
   145  				Discarded: 4,
   146  				Labels:    []string{"worker 1"},
   147  				Error:     errors.New("worker 1 error"),
   148  			},
   149  			wait: []int{0, 1, 2},
   150  		},
   151  	}
   152  
   153  	for specIdx, spec := range specs {
   154  		parameters.Workers = spec.workers
   155  
   156  		testRunner := &runner{
   157  			parameters: parameters,
   158  			worker: func(num int, shouldStop shouldStop) *TestResult {
   159  				if num < len(spec.wait) {
   160  					time.Sleep(time.Duration(spec.wait[num]) * time.Second)
   161  				}
   162  
   163  				if num < len(spec.res) {
   164  					return &spec.res[num]
   165  				}
   166  
   167  				return &spec.res[0]
   168  			},
   169  		}
   170  
   171  		result := testRunner.runWorkers()
   172  
   173  		if result.Time <= 0 {
   174  			t.Errorf("[%d] expected result time to be positive number but got %s", specIdx, result.Time)
   175  		}
   176  
   177  		// This is not deterministic and
   178  		// have validated above the time
   179  		result.Time = 0
   180  
   181  		if !reflect.DeepEqual(result, spec.exp) {
   182  			t.Errorf("[%d] expected test result %#v but got %#v",
   183  				specIdx,
   184  				spec.exp,
   185  				result,
   186  			)
   187  		}
   188  	}
   189  }