github.com/theQRL/go-zond@v0.1.1/common/mclock/simclock_test.go (about)

     1  // Copyright 2019 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  	var (
    29  		timeout = 30 * time.Minute
    30  		offset  = 99 * time.Hour
    31  		adv     = 11 * time.Minute
    32  		c       Simulated
    33  	)
    34  	c.Run(offset)
    35  
    36  	end := c.Now().Add(timeout)
    37  	ch := c.After(timeout)
    38  	for c.Now() < end.Add(-adv) {
    39  		c.Run(adv)
    40  		select {
    41  		case <-ch:
    42  			t.Fatal("Timer fired early")
    43  		default:
    44  		}
    45  	}
    46  
    47  	c.Run(adv)
    48  	select {
    49  	case stamp := <-ch:
    50  		want := AbsTime(0).Add(offset).Add(timeout)
    51  		if stamp != want {
    52  			t.Errorf("Wrong time sent on timer channel: got %v, want %v", stamp, want)
    53  		}
    54  	default:
    55  		t.Fatal("Timer didn't fire")
    56  	}
    57  }
    58  
    59  func TestSimulatedAfterFunc(t *testing.T) {
    60  	var c Simulated
    61  
    62  	called1 := false
    63  	timer1 := c.AfterFunc(100*time.Millisecond, func() { called1 = true })
    64  	if c.ActiveTimers() != 1 {
    65  		t.Fatalf("%d active timers, want one", c.ActiveTimers())
    66  	}
    67  	if fired := timer1.Stop(); !fired {
    68  		t.Fatal("Stop returned false even though timer didn't fire")
    69  	}
    70  	if c.ActiveTimers() != 0 {
    71  		t.Fatalf("%d active timers, want zero", c.ActiveTimers())
    72  	}
    73  	if called1 {
    74  		t.Fatal("timer 1 called")
    75  	}
    76  	if fired := timer1.Stop(); fired {
    77  		t.Fatal("Stop returned true after timer was already stopped")
    78  	}
    79  
    80  	called2 := false
    81  	timer2 := c.AfterFunc(100*time.Millisecond, func() { called2 = true })
    82  	c.Run(50 * time.Millisecond)
    83  	if called2 {
    84  		t.Fatal("timer 2 called")
    85  	}
    86  	c.Run(51 * time.Millisecond)
    87  	if !called2 {
    88  		t.Fatal("timer 2 not called")
    89  	}
    90  	if fired := timer2.Stop(); fired {
    91  		t.Fatal("Stop returned true after timer has fired")
    92  	}
    93  }
    94  
    95  func TestSimulatedSleep(t *testing.T) {
    96  	var (
    97  		c       Simulated
    98  		timeout = 1 * time.Hour
    99  		done    = make(chan AbsTime, 1)
   100  	)
   101  	go func() {
   102  		c.Sleep(timeout)
   103  		done <- c.Now()
   104  	}()
   105  
   106  	c.WaitForTimers(1)
   107  	c.Run(2 * timeout)
   108  	select {
   109  	case stamp := <-done:
   110  		want := AbsTime(2 * timeout)
   111  		if stamp != want {
   112  			t.Errorf("Wrong time after sleep: got %v, want %v", stamp, want)
   113  		}
   114  	case <-time.After(5 * time.Second):
   115  		t.Fatal("Sleep didn't return in time")
   116  	}
   117  }
   118  
   119  func TestSimulatedTimerReset(t *testing.T) {
   120  	var (
   121  		c       Simulated
   122  		timeout = 1 * time.Hour
   123  	)
   124  	timer := c.NewTimer(timeout)
   125  	c.Run(2 * timeout)
   126  	select {
   127  	case ftime := <-timer.C():
   128  		if ftime != AbsTime(timeout) {
   129  			t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(timeout))
   130  		}
   131  	default:
   132  		t.Fatal("timer didn't fire")
   133  	}
   134  
   135  	timer.Reset(timeout)
   136  	c.Run(2 * timeout)
   137  	select {
   138  	case ftime := <-timer.C():
   139  		if ftime != AbsTime(3*timeout) {
   140  			t.Fatalf("wrong time %v sent on timer channel, want %v", ftime, AbsTime(3*timeout))
   141  		}
   142  	default:
   143  		t.Fatal("timer didn't fire again")
   144  	}
   145  }
   146  
   147  func TestSimulatedTimerStop(t *testing.T) {
   148  	var (
   149  		c       Simulated
   150  		timeout = 1 * time.Hour
   151  	)
   152  	timer := c.NewTimer(timeout)
   153  	c.Run(2 * timeout)
   154  	if timer.Stop() {
   155  		t.Errorf("Stop returned true for fired timer")
   156  	}
   157  	select {
   158  	case <-timer.C():
   159  	default:
   160  		t.Fatal("timer didn't fire")
   161  	}
   162  }