|
Dlin |
at May 14, 2013 at 6:25 am
|
⇧ |
| |
I shorted it too much. And, now my improved version have passed the Go
1.1 new feature -race test.
::::::::::::::
multi.go
::::::::::::::
package multi
import (
"time"
"sync"
)
type Server struct {
count int
mu sync.RWMutex
}
func (s *Server) GetCount() int {
s.mu.RLock()
defer s.mu.RUnlock()
return s.count
}
func (s *Server) Serve() {
for {
time.Sleep(1 * time.Second)
s.mu.Lock()
s.count++
s.mu.Unlock()
time.Sleep(2 * time.Second)
s.mu.Lock()
s.count--
s.mu.Unlock()
time.Sleep(1 * time.Hour)
}
}
::::::::::::::
multi_test.go
::::::::::::::
package multi
import (
"testing"
"time"
)
func TestMulti(t *testing.T) {
srv := &Server{}
go func() {srv.Serve()}()
time.Sleep(2 * time.Second)
n := srv.GetCount()
if n != 1 {
t.Fatal("server should be 1 after 2 second, but got", n)
}
time.Sleep(2 * time.Second)
n = srv.GetCount()
if n != 0 {
t.Fatal("server should be 0 after 2 second, but got", n)
}
*******************
Here is the running log.
*******************
14:19:21$ go build
14:19:25$ go test
PASS
ok _/home/dlin/tmp/multi 4.023s
14:19:31$ go test -race
warning: building out-of-date packages: ( I don't know why, I build Go by
myself)
runtime
runtime/cgo
runtime/race
sync/atomic
sync
errors
syscall
time
io
unicode
unicode/utf8
bytes
math
os
strconv
reflect
fmt
sort
flag
bufio
strings
text/tabwriter
runtime/pprof
testing
regexp/syntax
regexp
installing these packages with 'go test -race -i' will speed future tests.
PASS
ok _/home/dlin/tmp/multi 5.038s
On Tuesday, May 14, 2013 2:07:20 PM UTC+8, Dave Cheney wrote:Hi dlin,
You need to reread the go memory model document, the sample you posted has
a data race.
On 13/05/2013, at 22:59, dlin <
[email protected] <javascript:>> wrote:
Hi, Dave,
I use following simple test to get a idea. (This test works).
Maybe it is caused by WaitGroup. I should use simpler logic to solve it.
::::::::::::::
multi.go
::::::::::::::
package multi
import (
"time"
)
type Server struct {
Count int
}
func (s *Server) Serve() {
for {
time.Sleep(1 * time.Second)
s.Count++
time.Sleep(2 * time.Second)
s.Count--
time.Sleep(1 * time.Hour)
}
}
::::::::::::::
multi_test.go
::::::::::::::
package multi
import (
"testing"
"time"
)
func TestMulti(t *testing.T) {
srv := &Server{}
go func() {srv.Serve()}()
time.Sleep(2 * time.Second)
if srv.Count != 1 {
t.Fatal("server should be 1 after 2 second")
}
time.Sleep(2 * time.Second)
if srv.Count != 0 {
t.Fatal("server should be 0 after 2 second")
}
}
On Tuesday, May 14, 2013 12:04:43 PM UTC+8, dlin wrote:
I wrote a client() and server() socket function. But in the following
logic, it won't quick as soon as my test.
I wish I could test on 2 seconds if numClient connected.
wg sync.WaitGroup
wg.Add(1)
srv = NewServer()
go func() {
srv.server() // server will run at least 5 seconds
wg.Done()
}
numClient := 1000
for i:=0; i< numClient; i++ {
wg.Add(1)
go func() {
client() // client will run at least 3 seconds
wg.Done()
}
}
go func() {
time.Sleep(2 * time.Second)
n := srv.GetOnlineCount()
if n != 1 {
t.Fatal("client not connected")
}
}
wg.Wait()
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to
[email protected] <javascript:>.
For more options, visit
https://groups.google.com/groups/opt_out. --
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
[email protected].
For more options, visit
https://groups.google.com/groups/opt_out.