Skip to content

Commit

Permalink
Panics When Shutdown inside new funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
tung.tq committed Oct 24, 2023
1 parent 68b10cc commit 6c02de2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
## Installtion

```bash
go get github.com/QuangTung97/svloc@v0.4.0
go get github.com/QuangTung97/svloc@v0.5.0
```

## Examples
Expand Down
4 changes: 4 additions & 0 deletions svloc.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ func (u *universeData) cloneShutdownFuncList() []func() {
// This function must only be called outside the 'new' functions.
// It will panic if called inside
func (u *Universe) Shutdown() {
if u.prev != nil {
panic("svloc: can NOT call Shutdown inside new functions")
}

funcList := u.data.cloneShutdownFuncList()

for i := len(funcList) - 1; i >= 0; i-- {
Expand Down
13 changes: 13 additions & 0 deletions svloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,3 +1031,16 @@ func TestUniverse_getPrintLocations(t *testing.T) {
func assertSuffixEqual(t *testing.T, suffix string, s string) {
assert.Equal(t, suffix, s[len(s)-len(suffix):])
}

func TestLocator_Do_Shutdown__Panics(t *testing.T) {
repoLoc := Register[Repo](func(unv *Universe) Repo {
unv.Shutdown()
return &UserRepo{}
})

unv := NewUniverse()

assert.PanicsWithValue(t, "svloc: can NOT call Shutdown inside new functions", func() {
repoLoc.Get(unv)
})
}

0 comments on commit 6c02de2

Please sign in to comment.