github.com/andy2046/gopie@v0.7.0/pkg/deadline/deadline_test.go (about)

     1  package deadline
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  var (
    10  	errText = "xxx"
    11  )
    12  
    13  func TestDeadline(t *testing.T) {
    14  	d := New(10 * time.Millisecond)
    15  
    16  	if err := d.Go(fiveMillisecondFunc); err != nil {
    17  		t.Error(err)
    18  	}
    19  
    20  	if err := d.Go(twentyMillisecondFunc); err != ErrTimeout {
    21  		t.Error(err)
    22  	}
    23  
    24  	if err := d.Go(errorFunc); err.Error() != errText {
    25  		t.Error(err)
    26  	}
    27  }
    28  
    29  func fiveMillisecondFunc(<-chan struct{}) error {
    30  	time.Sleep(5 * time.Millisecond)
    31  	return nil
    32  }
    33  
    34  func twentyMillisecondFunc(<-chan struct{}) error {
    35  	time.Sleep(20 * time.Millisecond)
    36  	return nil
    37  }
    38  
    39  func errorFunc(<-chan struct{}) error {
    40  	return errors.New(errText)
    41  }