github.com/chainopen/ethchaincode@v0.0.0-20190924072703-d975acdaa1c6/common/mclock/simclock_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package mclock
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  var _ Clock = System{}
    25  var _ Clock = new(Simulated)
    26  
    27  func TestSimulatedAfter(t *testing.T) {
    28  	const timeout = 30 * time.Minute
    29  	const adv = time.Minute
    30  
    31  	var (
    32  		c   Simulated
    33  		end = c.Now().Add(timeout)
    34  		ch  = c.After(timeout)
    35  	)
    36  	for c.Now() < end.Add(-adv) {
    37  		c.Run(adv)
    38  		select {
    39  		case <-ch:
    40  			t.Fatal("Timer fired early")
    41  		default:
    42  		}
    43  	}
    44  
    45  	c.Run(adv)
    46  	select {
    47  	case stamp := <-ch:
    48  		want := time.Time{}.Add(timeout)
    49  		if !stamp.Equal(want) {
    50  			t.Errorf("Wrong time sent on timer channel: got %v, want %v", stamp, want)
    51  		}
    52  	default:
    53  		t.Fatal("Timer didn't fire")
    54  	}
    55  }
    56  
    57  func TestSimulatedAfterFunc(t *testing.T) {
    58  	var c Simulated
    59  
    60  	called1 := false
    61  	timer1 := c.AfterFunc(100*time.Millisecond, func() { called1 = true })
    62  	if c.ActiveTimers() != 1 {
    63  		t.Fatalf("%d active timers, want one", c.ActiveTimers())
    64  	}
    65  	if fired := timer1.Stop(); !fired {
    66  		t.Fatal("Stop returned false even though timer didn't fire")
    67  	}
    68  	if c.ActiveTimers() != 0 {
    69  		t.Fatalf("%d active timers, want zero", c.ActiveTimers())
    70  	}
    71  	if called1 {
    72  		t.Fatal("timer 1 called")
    73  	}
    74  	if fired := timer1.Stop(); fired {
    75  		t.Fatal("Stop returned true after timer was already stopped")
    76  	}
    77  
    78  	called2 := false
    79  	timer2 := c.AfterFunc(100*time.Millisecond, func() { called2 = true })
    80  	c.Run(50 * time.Millisecond)
    81  	if called2 {
    82  		t.Fatal("timer 2 called")
    83  	}
    84  	c.Run(51 * time.Millisecond)
    85  	if !called2 {
    86  		t.Fatal("timer 2 not called")
    87  	}
    88  	if fired := timer2.Stop(); fired {
    89  		t.Fatal("Stop returned true after timer has fired")
    90  	}
    91  }
    92  
    93  func TestSimulatedSleep(t *testing.T) {
    94  	var (
    95  		c       Simulated
    96  		timeout = 1 * time.Hour
    97  		done    = make(chan AbsTime)
    98  	)
    99  	go func() {
   100  		c.Sleep(timeout)
   101  		done <- c.Now()
   102  	}()
   103  
   104  	c.WaitForTimers(1)
   105  	c.Run(2 * timeout)
   106  	select {
   107  	case stamp := <-done:
   108  		want := AbsTime(2 * timeout)
   109  		if stamp != want {
   110  			t.Errorf("Wrong time after sleep: got %v, want %v", stamp, want)
   111  		}
   112  	case <-time.After(5 * time.Second):
   113  		t.Fatal("Sleep didn't return in time")
   114  	}
   115  }