Merge branch 'state-races' into state-copy

Test that races found in old code are no longer found in new code.
This commit is contained in:
Alex Bramley 2014-12-31 17:32:15 +00:00
commit c5830d598f
4 changed files with 74 additions and 5 deletions

View File

@ -96,8 +96,14 @@ func (conn *Conn) User(ident, name string) {
conn.Raw(USER + " " + ident + " 12 * :" + name) conn.Raw(USER + " " + ident + " 12 * :" + name)
} }
// Join() sends a JOIN command to the server // Join() sends a JOIN command to the server with an optional key
func (conn *Conn) Join(channel string) { conn.Raw(JOIN + " " + channel) } func (conn *Conn) Join(channel string, key ...string) {
k := ""
if len(key) > 0 {
k = " " + key[0]
}
conn.Raw(JOIN + " " + channel + k)
}
// Part() sends a PART command to the server with an optional part message // Part() sends a PART command to the server with an optional part message
func (conn *Conn) Part(channel string, message ...string) { func (conn *Conn) Part(channel string, message ...string) {

View File

@ -97,6 +97,8 @@ func TestClientCommands(t *testing.T) {
c.Join("#foo") c.Join("#foo")
s.nc.Expect("JOIN #foo") s.nc.Expect("JOIN #foo")
c.Join("#foo bar")
s.nc.Expect("JOIN #foo bar")
c.Part("#foo") c.Part("#foo")
s.nc.Expect("PART #foo") s.nc.Expect("PART #foo")

View File

@ -83,6 +83,9 @@ type Config struct {
// Split PRIVMSGs, NOTICEs and CTCPs longer than // Split PRIVMSGs, NOTICEs and CTCPs longer than
// SplitLen characters over multiple lines. // SplitLen characters over multiple lines.
SplitLen int SplitLen int
// Timeout, The amount of time in seconds until a timeout is triggered.
Timeout time.Duration
} }
func NewConfig(nick string, args ...string) *Config { func NewConfig(nick string, args ...string) *Config {
@ -92,6 +95,7 @@ func NewConfig(nick string, args ...string) *Config {
NewNick: func(s string) string { return s + "_" }, NewNick: func(s string) string { return s + "_" },
Recover: (*Conn).LogPanic, // in dispatch.go Recover: (*Conn).LogPanic, // in dispatch.go
SplitLen: 450, SplitLen: 450,
Timeout: 60 * time.Second,
} }
cfg.Me.Ident = "goirc" cfg.Me.Ident = "goirc"
if len(args) > 0 && args[0] != "" { if len(args) > 0 && args[0] != "" {
@ -124,6 +128,7 @@ func Client(cfg *Config) *Conn {
} }
dialer := new(net.Dialer) dialer := new(net.Dialer)
dialer.Timeout = cfg.Timeout
if cfg.LocalAddr != "" { if cfg.LocalAddr != "" {
if !hasPort(cfg.LocalAddr) { if !hasPort(cfg.LocalAddr) {
cfg.LocalAddr += ":0" cfg.LocalAddr += ":0"

View File

@ -1,6 +1,9 @@
package state package state
import "testing" import (
"sync"
"testing"
)
// There is some awkwardness in these tests. Items retrieved directly from the // There is some awkwardness in these tests. Items retrieved directly from the
// state trackers internal maps are private and only have private, // state trackers internal maps are private and only have private,
@ -560,3 +563,56 @@ func TestSTWipe(t *testing.T) {
t.Errorf("Nick chan lists wrong length after wipe.") t.Errorf("Nick chan lists wrong length after wipe.")
} }
} }
func TestSTRaces(t *testing.T) {
st := NewTracker("mynick")
wg := sync.WaitGroup{}
for i := 'a'; i < 'g'; i++ {
wg.Add(2)
go func(s string) {
st.NewNick("nick-" + s)
c := st.NewChannel("#chan-" + s)
st.Associate(c, st.me)
wg.Done()
}(string(i))
go func(s string) {
n := st.GetNick("nick-" + s)
c := st.GetChannel("#chan-" + s)
st.Associate(c, n)
wg.Done()
}(string(i))
}
wg.Wait()
wg = sync.WaitGroup{}
race := func(ns, cs string) {
wg.Add(5)
go func() {
st.Associate(st.GetChannel("#chan-"+cs), st.GetNick("nick-"+ns))
wg.Done()
}()
go func() {
st.GetNick("nick-"+ns).Channels()
wg.Done()
}()
go func() {
st.GetChannel("#chan-"+cs).Nicks()
wg.Done()
}()
go func() {
st.Dissociate(st.GetChannel("#chan-"+cs), st.GetNick("nick-"+ns))
wg.Done()
}()
go func() {
st.ReNick("nick-"+ns, "nick2-"+ns)
wg.Done()
}()
}
for n := 'a'; n < 'g'; n++ {
for c := 'a'; c < 'g'; c++ {
race(string(n), string(c))
}
}
wg.Wait()
}