...

Source file src/github.com/dlclark/regexp2/fastclock_test.go

Documentation: github.com/dlclark/regexp2

     1  package regexp2
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func init() {
    10  	//speed up testing by making the timeout clock 1ms instead of 100ms...
    11  	//bad for benchmark tests though
    12  	SetTimeoutCheckPeriod(time.Millisecond)
    13  }
    14  func TestDeadline(t *testing.T) {
    15  	for _, delay := range []time.Duration{
    16  		clockPeriod / 10,
    17  		clockPeriod,
    18  		clockPeriod * 5,
    19  		clockPeriod * 10,
    20  	} {
    21  		delay := delay // Make copy for parallel sub-test.
    22  		t.Run(fmt.Sprint(delay), func(t *testing.T) {
    23  			t.Parallel()
    24  			start := time.Now()
    25  			d := makeDeadline(delay)
    26  			if d.reached() {
    27  				t.Fatalf("deadline (%v) unexpectedly expired immediately", delay)
    28  			}
    29  			time.Sleep(delay / 2)
    30  			if d.reached() {
    31  				t.Fatalf("deadline (%v) expired too soon (after %v)", delay, time.Since(start))
    32  			}
    33  			time.Sleep(delay/2 + 2*clockPeriod) // Give clock time to tick
    34  			if !d.reached() {
    35  				t.Fatalf("deadline (%v) did not expire within %v", delay, time.Since(start))
    36  			}
    37  		})
    38  	}
    39  }
    40  
    41  func TestStopTimeoutClock(t *testing.T) {
    42  	// run a quick regex with a long timeout
    43  	// make sure the stop clock returns quickly
    44  	r := MustCompile(".", 0)
    45  	r.MatchTimeout = time.Second * 10
    46  
    47  	r.MatchString("a")
    48  	start := time.Now()
    49  	StopTimeoutClock()
    50  	stop := time.Now()
    51  
    52  	if want, got := clockPeriod*2, stop.Sub(start); want < got {
    53  		t.Errorf("Expected duration less than %v, got %v", want, got)
    54  	}
    55  	if want, got := false, fast.running; want != got {
    56  		t.Errorf("Expected isRunning to be %v, got %v", want, got)
    57  	}
    58  }
    59  

View as plain text