Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when defer func is executed at ginkgo

I'm rewriting unit test of our k8s controller with ginkgo.

As previous TDD, for each test, we will have something like.

    // Create the Channel  object and expect the Reconcile
    g.Expect(c.Create(context.TODO(), myObj)).NotTo(gomega.HaveOccurred())
    defer c.Delete(context.TODO(),myObj)

What we want is, create an object for a test and delete it from the underline cluster after this test.

Now, on ginkgo, we are running tests within spec of containers. To me the container is the origin process, if that's the case does it mean the defer defined within the It spec will execute before exiting container instead of exiting the It spec.

For example,

var _ = Describe("my desr", func(){
   It("a", func(){
     fmt.Println(100)
     defer func(){fmt.Println("a", 100)}()
   })

   It("b", func(){
     fmt.Println(200)
     defer func(){fmt.Println("b", 200)}()
  })
})

Would the result be:

a

100
a100
200
b200

Or

b

100
200
b200
a100

In my case, I definitely what the first behavior. Or I'm in the wrong direction in terms of getting the defer behavior? I mean, should I look into the BeforeEach and AfterEach approach?

like image 415
Ian Zhang Avatar asked Dec 09 '25 21:12

Ian Zhang


2 Answers

You would see the first behavior, because the enclosing scope of the defers is the same anonymous function as the previous Println. deferred functions get called when exiting the scope.

like image 104
Oppen Avatar answered Dec 12 '25 15:12

Oppen


"Origin process" has nothing to do with it. Defer statements run when the surrounding function returns. In your case, the surrounding function is your anonymous func() passed to (and called by) It.

like image 45
hobbs Avatar answered Dec 12 '25 15:12

hobbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!