Skip to content

hsleep/go-recoverable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-recoverable

WaitGroup

It has similar interface with sync.WaitGroup. But it will call recover() function in WaitGroup.Done() function to recover panic in goroutine. And WaitGroup.Wait() function will raise panic if any panic occurs in WaitGroup.Done(). The panic by raised WaitGroup.Wait() can be recovered by caller function.

Limitations

DO NOT reuse WaitGroup after WaitGroup.Wait() is called.

Example

func TestWaitGroup_Wait(t *testing.T) {
    t.Run("no panic", func(t *testing.T) {
        as := assert.New(t)

        var wg WaitGroup
        wg.Add(1)
        go func() {
            defer wg.Done()
        }()
        as.NotPanics(wg.Wait)
    })

    t.Run("two panics", func(t *testing.T) {
        as := assert.New(t)

        as.Panics(func() {
            var wg WaitGroup
            wg.Add(1)
            go func() {
                defer wg.Done()
            }()
            wg.Add(1)
            go func() {
                defer wg.Done()
                panic("panic 1")
            }()
            wg.Add(1)
            go func() {
                defer wg.Done()
                panic("panic 2")
            }()
            wg.Wait()
        })
    })
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages