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

Candidates & Contexts Code Samples #6

Open
dm03514 opened this issue May 8, 2021 · 0 comments
Open

Candidates & Contexts Code Samples #6

dm03514 opened this issue May 8, 2021 · 0 comments

Comments

@dm03514
Copy link
Owner

dm03514 commented May 8, 2021

HTTP Handler - Non-Synchronized Counter

	counter := 0

	go func() {
		http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			counter = counter + 1 # line 28
			fmt.Printf("handling request: %d\n", counter)
			fmt.Fprintln(w, "Hello, client")
		}))
		log.Fatal(http.ListenAndServe(":8080", nil))
	}()

Explicit Race Condition Test - Counting to 10000 - 200 concurrent requesters

$ go test -run TestExplicitRace ./races/ -v -total-requests=10000 -concurrent-requests=200
=== RUN   TestExplicitRace
Num Requests TO Make: 10000
Final Count: 9884
    TestExplicitRace: explicit_test.go:69: expected 10000 requests: received 9884
--- FAIL: TestExplicitRace (1.35s)
FAIL
FAIL    github.com/dm03514/grokking-go/candidates-and-contexts/races    1.475s
FAIL

Explicit Race - Race Detector

$ go test -run TestExplicitRace ./races/ -v -total-requests=10000 -concurrent-requests=200 -race
=== RUN   TestExplicitRace
==================
WARNING: DATA RACE
Read at 0x00c000128350 by goroutine 550:
  github.com/dm03514/grokking-go/candidates-and-contexts/races.TestExplicitRace.func1.1()
      /Users/danielmican/go/src/github.com/dm03514/grokking-go/candidates-and-contexts/races/explicit_test.go:28 +0x4a
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2012 +0x51
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2387 +0x288
  net/http.serverHandler.ServeHTTP()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2807 +0xce
  net/http.(*conn).serve()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:1895 +0x837

Previous write at 0x00c000128350 by goroutine 491:
  github.com/dm03514/grokking-go/candidates-and-contexts/races.TestExplicitRace.func1.1()
      /Users/danielmican/go/src/github.com/dm03514/grokking-go/candidates-and-contexts/races/explicit_test.go:28 +0x60
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2012 +0x51
  net/http.(*ServeMux).ServeHTTP()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2387 +0x288
  net/http.serverHandler.ServeHTTP()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:2807 +0xce
  net/http.(*conn).serve()
      /usr/local/Cellar/go/1.14.4/libexec/src/net/http/server.go:1895 +0x837

HTTP Handler - Synchronized Counter - SUCCESS

	var mu = new(sync.Mutex)
	counter := 0

	go func() {
		http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			mu.Lock()
			defer mu.Unlock()
			counter = counter + 1

			fmt.Fprintln(w, "Hello, client")
		}))
		log.Fatal(http.ListenAndServe(":8080", nil))
	}()
$ go test -run TestSynchronizedMutexNoRace ./races/ -v -total-requests=10000 -concurrent-requests=200

=== RUN   TestSynchronizedMutexNoRace
Num Requests TO Make: 10000
Final Count: 10000
--- PASS: TestSynchronizedMutexNoRace (0.86s)
PASS
ok      github.com/dm03514/grokking-go/candidates-and-contexts/races    0.986s

Race Detector

$ go test -run TestSynchronizedMutexNoRace ./races/ -v -total-requests=10000 -concurrent-requests=200 -race
=== RUN   TestSynchronizedMutexNoRace
Num Requests TO Make: 10000
Final Count: 10000
--- PASS: TestSynchronizedMutexNoRace (4.27s)
PASS
ok      github.com/dm03514/grokking-go/candidates-and-contexts/races    4.476s

HTTP Handler - Monitor Pattern - SUCCESS

type CounterMonitor struct {
	count int
}

func (cm *CounterMonitor) Monitor(c chan struct{}) {
	for range c {
		cm.count = cm.count + 1
	}
}

func (cm *CounterMonitor) Count() int {
	return cm.count
}

	countChan := make(chan struct{})

	cm := &CounterMonitor{}
	go cm.Monitor(countChan)

	go func() {
		http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprintln(w, "Hello, client")
			countChan <- struct{}{}
		}))
		log.Fatal(http.ListenAndServe(":8080", nil))
	}()

Monitor Test - SUCCESS! - Multiple Concurrent Producers - Single Consumer

$ go test -run TestMonitorNoRace ./races/ -v -total-requests=10000 -concurrent-requests=200
=== RUN   TestMonitorNoRace
Num Requests TO Make: 10000
Final Count: 10000
--- PASS: TestMonitorNoRace (0.68s)
PASS
ok      github.com/dm03514/grokking-go/candidates-and-contexts/races    0.804s
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

No branches or pull requests

1 participant