Skip to content

Instantly share code, notes, and snippets.

@patryk4815
Created July 28, 2020 12:06
Show Gist options
  • Save patryk4815/3b04ef06726af3610e4d7fbca10b62f5 to your computer and use it in GitHub Desktop.
Save patryk4815/3b04ef06726af3610e4d7fbca10b62f5 to your computer and use it in GitHub Desktop.
package x2
import (
"fmt"
"sync"
"testing"
)
type simpleDeadlock struct {
mu *sync.RWMutex
list []int
}
func (s *simpleDeadlock) FillCache() error {
s.mu.Lock()
defer s.mu.Unlock()
s.list = []int{0,1,2,3,4}
return nil
}
func (s *simpleDeadlock) GetCache() error {
s.mu.RLock()
defer s.mu.RUnlock()
for _, i := range s.list {
s.checkCache(i)
}
return nil
}
func (s *simpleDeadlock) checkCache(i int) int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.list[i]
}
func TestDeadlock(t *testing.T) {
srv := &simpleDeadlock{
mu: &sync.RWMutex{},
}
go func() {
i := 0
for {
i++
fmt.Println("FillCache", i)
srv.FillCache()
}
}()
i := 0
for {
i++
fmt.Println("GetCache", i)
srv.GetCache()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment