mercredi 6 janvier 2021

async testing with Gingko and Gomega

I have expanded the following code as per my requirement. https://github.com/radovskyb/watcher/blob/master/example/basics/main.go

Purpose of this code is to watch of certain events (CREATE, WRITE) and take appropriate action based on that. To test this code I am using Ginkgo and Gomega and this is where I am looking for help.

I am trying to test below peace of code (Also provided in link) and I am not sure how should I proceed.

    go func() {
        for {
            select {
            case event := <-w.Event:
                fmt.Println(event) // Print the event's info.
            case err := <-w.Error:
                log.Fatalln(err)
            case <-w.Closed:
                return
            }
        }
    }()

I checked, Gomega has support for async testing which supports "Channel" but I tried couple of things but I ended up initialising new watcher object which require actual event to be triggered for testing. I tried to create "tempDir" so I can trigger CRETE / WRITE event and then close the channel but its not helping and after running this particular test, it just wait for channel to be closed.

                w := watcher.New()
                w.FilterOps(watcher.Create, watcher.Write)

                dir, err := ioutil.TempDir("", "watcher2")
                Expect(err).To(BeNil())

                defer os.RemoveAll(dir)
                tmpfile := filepath.Join(dir, "watcherremoved")

                err = ioutil.WriteFile(tmpfile, []byte("init"), 0666)
                Expect(err).To(BeNil())

                event := <-w.Event
                Eventually(event.Path).Should(Equal(ADMIN_VAULT))
            
                <-w.Closed

Aucun commentaire:

Enregistrer un commentaire