检查函数是否未运行的测试?

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

Test to check if a function didn't run?

问题

所以我对测试还不太熟悉,我在尝试为一个触发另一个函数的函数编写测试时遇到了困难。这是我目前的代码,但如果函数没有运行,它会变得有些反向并且永远阻塞:

  1. var cha = make(chan bool, 1)
  2. func TestFd(t *testing.T) {
  3. c := &fd.Fdcount{Interval: 1, MaxFiles: 1}
  4. c.Start(trigger)
  5. if <-cha {
  6. }
  7. }
  8. func trigger(i int) {
  9. cha <- true
  10. }

当满足某些条件时,c.Start 将触发 trigger() 函数。它每隔 1 秒测试一次是否满足条件。

错误情况是函数无法运行。有没有办法测试这种情况,或者是否可以使用测试包来测试成功(例如 t.Pass())?

英文:

So I'm new to testing in general and I'm stuck trying to write a test for a function that triggers another function. This is what I have so far but it's kind of backwards and blocks forever if the function doesn't run:

  1. var cha = make(chan bool, 1)
  2. func TestFd(t *testing.T) {
  3. c := &amp;fd.Fdcount{Interval: 1, MaxFiles: 1}
  4. c.Start(trigger)
  5. if &lt;- cha {
  6. }
  7. }
  8. func trigger(i int) {
  9. cha &lt;- true
  10. }

c.Start will trigger the trigger() function when certain criteria is met. It tests whether or not the criteria is met every 1 second.

The error case is when the function fails to run. Is there a way to test for this or is there a way to use the testing package to test for success (e.g. t.Pass())?

答案1

得分: 2

如果c.Start是同步的,你可以简单地传递一个函数来设置测试用例范围内的一个值,然后根据该值进行测试。考虑下面示例中由trigger函数设置的functionCalled变量(playground):

  1. func TestFd(t *testing.T) {
  2. functionCalled := false
  3. trigger := func(i int) {
  4. functionCalled = true;
  5. }
  6. c := &fd.Fdcount{Interval: 1, MaxFiles: 1}
  7. c.Start(trigger)
  8. if !functionCalled {
  9. t.FatalF("function was not called")
  10. }
  11. }

如果c.Start是异步的,你可以使用select语句来实现一个超时,在给定的时间范围内未调用传递的函数时,测试将失败(playground):

  1. func TestFd(t *testing.T) {
  2. functionCalled := make(chan bool)
  3. timeoutSeconds := 1 * time.Second
  4. trigger := func(i int) {
  5. functionCalled <- true
  6. }
  7. timeout := time.After(timeoutSeconds)
  8. c := &SomeStruct{}
  9. c.Start(trigger)
  10. select {
  11. case <- functionCalled:
  12. t.Logf("function was called")
  13. case <- timeout:
  14. t.Fatalf("function was not called within timeout")
  15. }
  16. }
英文:

If c.Start is synchronous, you can simply pass a function that sets a value in the test case's scope, and then test against that value. Condider the functionCalled variable in the example below that is set by the trigger function (playground):

  1. func TestFd(t *testing.T) {
  2. functionCalled := false
  3. trigger := func(i int) {
  4. functionCalled = true;
  5. }
  6. c := &amp;fd.Fdcount{Interval: 1, MaxFiles: 1}
  7. c.Start(trigger)
  8. if !functionCalled {
  9. t.FatalF(&quot;function was not called&quot;)
  10. }
  11. }

If c.Start is asynchronous, you can use a select statement to implement a timeout that will fail the test when the passed function was not called within a given time frame (playground):

  1. func TestFd(t *testing.T) {
  2. functionCalled := make(chan bool)
  3. timeoutSeconds := 1 * time.Second
  4. trigger := func(i int) {
  5. functionCalled &lt;- true
  6. }
  7. timeout := time.After(timeoutSeconds)
  8. c := &amp;SomeStruct{}
  9. c.Start(trigger)
  10. select {
  11. case &lt;- functionCalled:
  12. t.Logf(&quot;function was called&quot;)
  13. case &lt;- timeout:
  14. t.Fatalf(&quot;function was not called within timeout&quot;)
  15. }
  16. }

huangapple
  • 本文由 发表于 2017年1月16日 00:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/41663322.html
匿名

发表评论

匿名网友

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

确定