如何在Golang中测试一个睡眠函数

huangapple go评论100阅读模式
英文:

How to test a sleep function in golang

问题

我已经编写了自己的睡眠函数并想要进行测试。以下是我的代码:

func TestSleep(t *testing.T) {
    start := time.Now()
    mySleepFunction(65)
    end := time.Now()
    if end.Sub(start).Seconds() != 65 {
        t.Error("Incorrect sleep function")
    }
}

这段代码没有起作用。我试图获取开始时间和结束时间,然后将其与预期时间进行比较。预期时间将以秒为单位。我尝试了end.Sub(start),但它返回了类似于1m30.0909的结果,而我需要的是90。如果有人能帮助我,那就太好了。谢谢 如何在Golang中测试一个睡眠函数

英文:

I have written my own sleep function and want to test it. Following is my code:

func TestSleep(t *testing.T) {
    start := time.Now()
    mySleepFunction(65)
    end := time.Now()
    if (end - start) != 65 {
	    t.Error("Incorrect sleep function")
    }
}

This is not working. I am trying to get start time and end time and then compare it with expected time. The expected time will be in seconds. I tried end.Sub(start) but this returns me something like 1m30.0909, instead I need 90 as a result. It would be great if someone could help me.
Thanks 如何在Golang中测试一个睡眠函数

答案1

得分: 15

经过的时间:

获取自特定时间(start)以来经过的时间最简单的方法是使用time.Since()函数,它返回一个time.Duration类型的值,该类型具有Duration.Seconds()方法,可以将持续时间以float64的形式返回为秒。

所以在你的情况下,经过的秒数为:

sec := time.Since(start).Seconds()

现在来进行测试...

在测试类似于sleep函数的功能时,你应该考虑到在休眠之后,不能保证代码会立即继续执行。例如,引用自time.Sleep()的文档:

> Sleep函数会暂停当前的goroutine,至少持续时间为d

因此,在编写针对类似于sleep函数的测试代码时,我会像这样进行测试:

  • 经过的时间应至少等于指定的时间,
  • 并允许一定的误差范围。

所以,例如,可以像这样进行测试:

func TestSleep(t *testing.T) {
    const secSleep = 65.0

    start := time.Now()
    mySleepFunction(int(secSleep))
    sec := time.Since(start).Seconds()

    if sec < secSleep || sec > secSleep*1.05 {
        t.Error("Incorrect sleep function")
    }
}
英文:

Elapsed time:

The easiest to get the elapsed time since a specific time (start) is to use the time.Since() function which returns a time.Duration which has a Duration.Seconds() method which returns the duration in seconds as float64.

So in your case the elapsed seconds:

sec := time.Since(start).Seconds()

Now on to testing...

When testing sleep-like functions you should take into consideration that after the sleep it is not guaranteed that the code will continue to execute immediately. For example quoting from the doc of time.Sleep():

> Sleep pauses the current goroutine for at least the duration d.

So when writing test code for sleep-like functions, I would test like this:

  • elapsed time should be at least the specified,
  • and allow some error margin.

So for example test it like this:

func TestSleep(t *testing.T) {
	const secSleep = 65.0

	start := time.Now()
	mySleepFunction(int(secSleep))
	sec := time.Since(start).Seconds()

	if sec &lt; secSleep || sec &gt; secSleep*1.05 {
		t.Error(&quot;Incorrect sleep function&quot;)
	}
}

答案2

得分: 1

如果你使用time.Unix()函数,它会给你返回自纪元(1970年1月1日UTC)以来的秒数,那么你的测试应该可以工作。

func TestSleep(t *testing.T) {
    start := time.Now().Unix()
    mySleepFunction(65)
    end := time.Now().Unix()
    if (end - start) != 65{
        t.Error("Incorrect sleep function")
    }
}

目前,你的代码在减去time.Now(),这不会返回你想要的结果。你的测试需要简单的整数值来表示秒数。

英文:

If you use time.Unix() which gives you the amount of seconds since the epoch (January 1, 1970 UTC.) then your test should work.

func TestSleep(t *testing.T) {
    start := time.Now().Unix()
    mySleepFunction(65)
    end := time.Now().Unix()
    if (end - start) != 65{
	    t.Error(&quot;Incorrect sleep function&quot;)
    }
}

Currently, your code is subtracting a time.Now() which doesn't return the result you intend. Your test wants simple int values that represent seconds.

huangapple
  • 本文由 发表于 2015年9月16日 11:09:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/32599117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定