Created
July 28, 2020 12:06
-
-
Save patryk4815/3b04ef06726af3610e4d7fbca10b62f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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