github.com/network-quality/goresponsiveness@v0.0.0-20240129151524-343954285090/executor/executor_test.go (about)

     1  package executor_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/network-quality/goresponsiveness/executor"
     8  )
     9  
    10  var countToFive = func() {
    11  	time.Sleep(5 * time.Second)
    12  }
    13  
    14  var countToThree = func() {
    15  	time.Sleep(3 * time.Second)
    16  }
    17  
    18  var executionUnits = []executor.ExecutionUnit{countToFive, countToThree}
    19  
    20  func TestSerial(t *testing.T) {
    21  	then := time.Now()
    22  	waiter := executor.Execute(executor.Serial, executionUnits)
    23  	waiter.Wait()
    24  	when := time.Now()
    25  
    26  	if when.Sub(then) < 7*time.Second {
    27  		t.Fatalf("Execution did not happen serially -- the wait was too short: %v", when.Sub(then).Seconds())
    28  	}
    29  }
    30  
    31  func TestParallel(t *testing.T) {
    32  	then := time.Now()
    33  	waiter := executor.Execute(executor.Parallel, executionUnits)
    34  	waiter.Wait()
    35  	when := time.Now()
    36  
    37  	if when.Sub(then) > 6*time.Second {
    38  		t.Fatalf("Execution did not happen in parallel -- the wait was too long: %v", when.Sub(then).Seconds())
    39  	}
    40  }
    41  
    42  func TestExecutionMethodParallelToString(t *testing.T) {
    43  	executionMethod := executor.Parallel
    44  
    45  	if executionMethod.ToString() != "Parallel" {
    46  		t.Fatalf("Incorrect result from ExecutionMethod.ToString; expected Parallel but got %v", executionMethod.ToString())
    47  	}
    48  }
    49  
    50  func TestExecutionMethodSerialToString(t *testing.T) {
    51  	executionMethod := executor.Serial
    52  
    53  	if executionMethod.ToString() != "Serial" {
    54  		t.Fatalf("Incorrect result from ExecutionMethod.ToString; expected Serial but got %v", executionMethod.ToString())
    55  	}
    56  }