-3

(This question is not about the playground per se, one commenter requested a playground, which is why I provided one. This question is about the testing feature of Go.)

I'm getting the time like this in a unit test: time.Now()

Here is some code showing how I get the time using time.Now():

package main

import (
    "log"
    "testing"
    "time"
)

// LastIndex returns the index of the last instance of x in list, or
// -1 if x is not present. The loop condition has a fault that
// causes some tests to fail. Change it to i >= 0 to see them pass.
func LastIndex(list []int, x int) int {
    for i := len(list) - 1; i > 0; i-- {
        if list[i] == x {
            return i
        }
    }
    return -1
}

func TestLastIndex(t *testing.T) {
    tests := []struct {
        list   []int
        x      int
        want   int
        mytime time.Time
    }{
        {list: []int{1}, x: 1, want: 0, mytime: time.Now()},
        {list: []int{1, 1}, x: 1, want: 1, mytime: time.Now()},
        {list: []int{2, 1}, x: 2, want: 0, mytime: time.Now()},
        {list: []int{1, 2, 1, 1}, x: 2, want: 1, mytime: time.Now()},
        {list: []int{1, 1, 1, 2, 2, 1}, x: 3, want: -1, mytime: time.Now()},
        {list: []int{3, 1, 2, 2, 1, 1}, x: 3, want: 0, mytime: time.Now()},
    }
    for _, tt := range tests {
        log.Println("Running test, mytime:", tt.mytime)
        if got := LastIndex(tt.list, tt.x); got != tt.want {
            t.Errorf("LastIndex(%v, %v) = %v, want %v", tt.list, tt.x, got, tt.want)
        }
    }
}

Despite calling the test several times, with a few seconds between, the "mytime" returned is exactly the same. Why is this?

(output redacted because some people misunderstand the question)

It seems a lot of you misunderstand the question completely. I'll state it like this again to make it easier to know what I mean:

  1. Execute the test and note the output times.
  2. Go grab a coffee.
  3. Execute the test again, and look at the times.

The times in step 1 and step 3 are the same.

Update: What seems to be a probable cause; the times given back by time.Now() is always going to be the time of code compilation, or something similar. Anyone can confirm this?

Jonny
  • 15,955
  • 18
  • 111
  • 232
  • 1
    It shouldn't, you need to provide minimal code exhibiting the behaviour to clarify your question. – davidriod Jul 20 '22 at 07:13
  • Here is a playground: https://go.dev/play/p/1VEHUXtss8N For some reason the playground always returns "2009-11-10 23:00:00 +0000 UTC m=+0.000000001" for the mytime. – Jonny Jul 20 '22 at 07:20
  • I'm not sure what you're asking. Your question still shows the printed timestamps from the Go Playground, which, as already stated, is fixed. – blackgreen Jul 20 '22 at 10:22
  • @blackgreen Please read the question, both the title and the body. The question has nothing to do with the playground, as I answered above. The playground was just introduced because mister Volker asked for it. – Jonny Jul 21 '22 at 00:32
  • @Jonny thanks for clarifying. I followed your steps. Ran the test on my machine, literally went pouring some coffee, came back and ran it again, and the output times are indeed different. So it could be that something is caching the test output during runs. – blackgreen Jul 21 '22 at 06:36
  • I updated the duplicate link, try force-disabling test caching as shown over there and see if the times are still the same – blackgreen Jul 21 '22 at 06:39

1 Answers1

0

You define the myTime-variables of the tests struct all at the start of the test. Therefore they all have the same time, because they are all intitiated with time.Now() in the same second all right after another.

Edit: Is it possible, that you are accessing cached testresults and you therefor get the same results despite running the test multiple times with delay?

You can refer to this question: Force retesting or disable test caching

From About the Playground: Edit: Seems like this part is not relevant for the answer.

In the playground the time begins at 2009-11-10 23:00:00 UTC (determining the significance of this date is an exercise for the reader). This makes it easier to cache programs by giving them deterministic output.

whatever.idc
  • 92
  • 1
  • 1
  • 10
  • Thanks, but the question was not about the playground in itself, but about Go tests in general. And the question is about different execution attempts, at least several seconds apart. Running tests outside the playground shows similar behaviour. Why is this? – Jonny Jul 20 '22 at 09:01
  • Because, as I stated in my answer, you define all `tests`-variables at the same time. Why do you expect them to be different? During the execution of the for-loop the time-variables are already set. I included the quote from the playground because I wasn`t sure, if you were surprised by the strange date aswell. – whatever.idc Jul 20 '22 at 09:11
  • Because I run the first test, wait a few seconds, then run the test again. – Jonny Jul 20 '22 at 09:20
  • https://go.dev/play/p/3u8D3bygB3u is this the functionality you expect? I don`t understand what you mean with running the test multiple times. That is not shown anywhere. Could you comment the code where exactly your are waiting in your provided code a few seconds? – whatever.idc Jul 20 '22 at 09:23
  • Let's put it such that you might have a better understanding: 1) Run test. 2) Go grab a coffee 3) Come back, and run the test again. << both executions in step 1 and step 3 shows the same time. Get it now? – Jonny Jul 21 '22 at 00:31
  • Okay - did you check if you are accessing cached testresults? – whatever.idc Jul 21 '22 at 04:48