FAQ
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].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at May 14, 2013 at 4:10 am
    In future, could you please post working examples as links to
    play.golang.org. It may be the difference between someone being able
    to run your program and reproduce the problem/question, or ignoring
    your email.
    On Tue, May 14, 2013 at 2:04 PM, 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].
    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.
  • Dlin at May 14, 2013 at 5:59 am
    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].
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Cheney at May 14, 2013 at 6:09 am
    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 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].
    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.
  • 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.
  • Peter Waller at May 16, 2013 at 11:12 pm

    On Tuesday, 14 May 2013 07:25:16 UTC+1, dlin wrote:

    14:19:31$ go test -race
    warning: building out-of-date packages: ( I don't know why, I build Go by
    myself)
    You need to "go install -race". The "-race" library binaries get installed
    to a directory named ${PLATFORM}_race instead of just ${PLATFORM}. Hence
    why it's building them every time, since they were never installed.

    --
    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.
  • Christoph Hack at May 16, 2013 at 11:22 pm
    Instead of protecting your counter with a mutex, you could also use the
    AddInt64 and LoadInt64 functions of the "sync/atomic" package. These are
    low-level synchronization primitives that are also used by the mutex
    implementation, but by using them directly, you can drastically improve the
    performance (and in this case also the complexity) of your program.

    -christoph

    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 14, '13 at 4:04a
activeMay 16, '13 at 11:22p
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase