Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add race conditions #6

Merged
merged 2 commits into from Nov 24, 2019
Merged

add race conditions #6

merged 2 commits into from Nov 24, 2019

Conversation

emahiro
Copy link
Owner

@emahiro emahiro commented Nov 24, 2019

これは何か?

Go で意図的に race conditions を発生させるサンプルです。
実装で concurrent map writes が発生するコードを仕込んでいるので test にて3並行処理を行い実際に concurrent map writes が発生するコードを作ります。

go test ./... -v
=== RUN   TestRace
fatal error: concurrent map writes

#

毎回ではないですが、競合状態を引き起こすテストになってます。

また race コンディションもチェックします。

go test ./... -race
key = test, val = 1
key = test, val = 3
m = map[test:1]
==================
WARNING: DATA RACE

#

意図通り発生します。

@emahiro
Copy link
Owner Author

emahiro commented Nov 24, 2019

これを解消するために並行処理において競合が発生しそうな場合には Lock を利用します。

@emahiro
Copy link
Owner Author

emahiro commented Nov 24, 2019

race conditions のチェックをします。

go test ./... -race  
ok      github.com/emahiro/raceTest     2.187s

問題なく通りました。

@emahiro emahiro merged commit 9de49cd into master Nov 24, 2019
@emahiro emahiro deleted the test/race_condition branch November 24, 2019 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant