Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/resourcestore/resourcestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ func (rc *ResourceStore) cleanupStaleResources() {
resourcesToReap := []*Resource{}
rc.Lock()
for name, r := range rc.resources {
// this resource shouldn't be marked as stale if it
// hasn't yet been added to the store.
// This can happen if a creation is in progress, and a watcher is added
// before the creation completes.
// If this resource isn't skipped from being marked as stale,
// we risk segfaulting in the Cleanup() step.
if !r.wasPut() {
continue
}
if r.stale {
resourcesToReap = append(resourcesToReap, r)
delete(rc.resources, name)
Expand Down Expand Up @@ -179,6 +188,7 @@ func (rc *ResourceStore) WatcherForResource(name string) chan struct{} {
if !ok {
rc.resources[name] = &Resource{
watchers: []chan struct{}{watcher},
name: name,
}
return watcher
}
Expand Down
20 changes: 20 additions & 0 deletions internal/resourcestore/resourcestore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,25 @@ var _ = t.Describe("ResourceStore", func() {
id := sut.Get(testName)
Expect(id).To(BeEmpty())
})
It("should not call cleanup until after resource is put", func() {
// Given
timeout := 2 * time.Second
sut = resourcestore.NewWithTimeout(timeout)

_ = sut.WatcherForResource(testName)

timedOutChan := make(chan bool)

// When
go func() {
time.Sleep(timeout * 6)
Expect(sut.Put(testName, e, cleaner)).To(BeNil())
timedOutChan <- true
}()

// Then
didStoreWaitForPut := <-timedOutChan
Expect(didStoreWaitForPut).To(Equal(true))
})
})
})