func main() { //simtime.SetCurrentTime(time.Now().Add(-time.Hour * 24 * 365 * 5)) go func() { log.Printf("1. Before day sleep: %s", time.Now()) simtime.Sleep(time.Hour * 24) log.Printf("1. After day sleep: %s", time.Now()) simtime.Continue() x := simtime.Tick(time.Hour) for t := range x { log.Println("1. -------") log.Printf("1. Fake Time : %s", t) log.Printf("1. Fake Now(): %s", simtime.Now()) log.Printf("1. Real Now(): %s", time.Now()) if simtime.Now() != t { panic("DIFFERENT!") } simtime.Continue() } }() go func() { x := simtime.Tick(time.Hour * 5) for t := range x { log.Println("2. -------") log.Printf("2. Fake Time : %s", t) log.Printf("2. Fake Now(): %s", simtime.Now()) log.Printf("2. Real Now(): %s", time.Now()) if simtime.Now() != t { panic("DIFFERENT!") } simtime.Continue() } }() c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, os.Kill) // Block until a signal is received. s := <-c fmt.Println("Got signal:", s) }
// CallWithTimeout invokes a function synchronously. func (client *Client) CallWithTimeout(topic string, serviceMethod string, args interface{}, reply interface{}, timeout time.Duration) error { call := &Call{ ID: rand.Uint32(), Topic: topic, ServiceMethod: serviceMethod, Args: args, Done: make(chan *Call, 1), Reply: reply, } err := client.send(call) if err != nil { return err } sentTime := simtime.Now() log.Debugf("id:%d - Waiting for reply", call.ID) select { case <-call.Done: log.Debugf("id:%d - Returned after %s", call.ID, time.Since(sentTime)) return call.Error case <-time.After(timeout): delete(client.pending, call.ID) return fmt.Errorf("id:%d - Call to service %s - (method: %s) timed out after %d seconds", call.ID, topic, serviceMethod, timeout/time.Second) } }
func makeTimestamp() int64 { return simtime.Now().UnixNano() / int64(time.Millisecond) }