Fix race in gateway test. (#520)

This commit is contained in:
Duco van Amstel 2018-10-12 23:47:18 +01:00 committed by Wim
parent e15b0e04b8
commit 3dd4ec57ff
1 changed files with 132 additions and 29 deletions

View File

@ -172,18 +172,27 @@ func TestNewRouter(t *testing.T) {
assert.Equal(t, 3, len(r.Gateways["bridge2"].Bridges)) assert.Equal(t, 3, len(r.Gateways["bridge2"].Bridges))
assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels)) assert.Equal(t, 4, len(r.Gateways["bridge1"].Channels))
assert.Equal(t, 3, len(r.Gateways["bridge2"].Channels)) assert.Equal(t, 3, len(r.Gateways["bridge2"].Channels))
assert.Equal(t, &config.ChannelInfo{Name: "42wim/testroom", Direction: "out", assert.Equal(t, &config.ChannelInfo{
ID: "42wim/testroomgitter.42wim", Account: "gitter.42wim", Name: "42wim/testroom",
SameChannel: map[string]bool{"bridge2": false}}, Direction: "out",
r.Gateways["bridge2"].Channels["42wim/testroomgitter.42wim"]) ID: "42wim/testroomgitter.42wim",
assert.Equal(t, &config.ChannelInfo{Name: "42wim/testroom", Direction: "in", Account: "gitter.42wim",
ID: "42wim/testroomgitter.42wim", Account: "gitter.42wim", SameChannel: map[string]bool{"bridge2": false},
SameChannel: map[string]bool{"bridge1": false}}, }, r.Gateways["bridge2"].Channels["42wim/testroomgitter.42wim"])
r.Gateways["bridge1"].Channels["42wim/testroomgitter.42wim"]) assert.Equal(t, &config.ChannelInfo{
assert.Equal(t, &config.ChannelInfo{Name: "general", Direction: "inout", Name: "42wim/testroom",
ID: "generaldiscord.test", Account: "discord.test", Direction: "in",
SameChannel: map[string]bool{"bridge1": false}}, ID: "42wim/testroomgitter.42wim",
r.Gateways["bridge1"].Channels["generaldiscord.test"]) Account: "gitter.42wim",
SameChannel: map[string]bool{"bridge1": false},
}, r.Gateways["bridge1"].Channels["42wim/testroomgitter.42wim"])
assert.Equal(t, &config.ChannelInfo{
Name: "general",
Direction: "inout",
ID: "generaldiscord.test",
Account: "discord.test",
SameChannel: map[string]bool{"bridge1": false},
}, r.Gateways["bridge1"].Channels["generaldiscord.test"])
} }
func TestGetDestChannel(t *testing.T) { func TestGetDestChannel(t *testing.T) {
@ -192,11 +201,23 @@ func TestGetDestChannel(t *testing.T) {
for _, br := range r.Gateways["bridge1"].Bridges { for _, br := range r.Gateways["bridge1"].Bridges {
switch br.Account { switch br.Account {
case "discord.test": case "discord.test":
assert.Equal(t, []config.ChannelInfo{{Name: "general", Account: "discord.test", Direction: "inout", ID: "generaldiscord.test", SameChannel: map[string]bool{"bridge1": false}, Options: config.ChannelOptions{Key: ""}}}, assert.Equal(t, []config.ChannelInfo{{
r.Gateways["bridge1"].getDestChannel(msg, *br)) Name: "general",
Account: "discord.test",
Direction: "inout",
ID: "generaldiscord.test",
SameChannel: map[string]bool{"bridge1": false},
Options: config.ChannelOptions{Key: ""},
}}, r.Gateways["bridge1"].getDestChannel(msg, *br))
case "slack.test": case "slack.test":
assert.Equal(t, []config.ChannelInfo{{Name: "testing", Account: "slack.test", Direction: "out", ID: "testingslack.test", SameChannel: map[string]bool{"bridge1": false}, Options: config.ChannelOptions{Key: ""}}}, assert.Equal(t, []config.ChannelInfo{{
r.Gateways["bridge1"].getDestChannel(msg, *br)) Name: "testing",
Account: "slack.test",
Direction: "out",
ID: "testingslack.test",
SameChannel: map[string]bool{"bridge1": false},
Options: config.ChannelOptions{Key: ""},
}}, r.Gateways["bridge1"].getDestChannel(msg, *br))
case "gitter.42wim": case "gitter.42wim":
assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br)) assert.Equal(t, []config.ChannelInfo(nil), r.Gateways["bridge1"].getDestChannel(msg, *br))
case "irc.freenode": case "irc.freenode":
@ -226,35 +247,87 @@ func TestGetDestChannelAdvanced(t *testing.T) {
} }
switch gw.Name { switch gw.Name {
case "bridge": case "bridge":
if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") && (msg.Account == "irc.zzz" || msg.Account == "telegram.zzz" || msg.Account == "slack.zzz") { if (msg.Channel == "#main" || msg.Channel == "-1111111111111" || msg.Channel == "irc") &&
(msg.Account == "irc.zzz" || msg.Account == "telegram.zzz" || msg.Account == "slack.zzz") {
hits[gw.Name]++ hits[gw.Name]++
switch br.Account { switch br.Account {
case "irc.zzz": case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main", Account: "irc.zzz", Direction: "inout", ID: "#mainirc.zzz", SameChannel: map[string]bool{"bridge": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "#main",
Account: "irc.zzz",
Direction: "inout",
ID: "#mainirc.zzz",
SameChannel: map[string]bool{"bridge": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
case "telegram.zzz": case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "-1111111111111", Account: "telegram.zzz", Direction: "inout", ID: "-1111111111111telegram.zzz", SameChannel: map[string]bool{"bridge": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "-1111111111111",
Account: "telegram.zzz",
Direction: "inout",
ID: "-1111111111111telegram.zzz",
SameChannel: map[string]bool{"bridge": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
case "slack.zzz": case "slack.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "irc", Account: "slack.zzz", Direction: "inout", ID: "ircslack.zzz", SameChannel: map[string]bool{"bridge": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "irc",
Account: "slack.zzz",
Direction: "inout",
ID: "ircslack.zzz",
SameChannel: map[string]bool{"bridge": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
} }
} }
case "bridge2": case "bridge2":
if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") && (msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") { if (msg.Channel == "#main-help" || msg.Channel == "--444444444444") &&
(msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") {
hits[gw.Name]++ hits[gw.Name]++
switch br.Account { switch br.Account {
case "irc.zzz": case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main-help", Account: "irc.zzz", Direction: "inout", ID: "#main-helpirc.zzz", SameChannel: map[string]bool{"bridge2": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "#main-help",
Account: "irc.zzz",
Direction: "inout",
ID: "#main-helpirc.zzz",
SameChannel: map[string]bool{"bridge2": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
case "telegram.zzz": case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "--444444444444", Account: "telegram.zzz", Direction: "inout", ID: "--444444444444telegram.zzz", SameChannel: map[string]bool{"bridge2": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "--444444444444",
Account: "telegram.zzz",
Direction: "inout",
ID: "--444444444444telegram.zzz",
SameChannel: map[string]bool{"bridge2": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
} }
} }
case "bridge3": case "bridge3":
if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") && (msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") { if (msg.Channel == "#main-telegram" || msg.Channel == "--333333333333") &&
(msg.Account == "irc.zzz" || msg.Account == "telegram.zzz") {
hits[gw.Name]++ hits[gw.Name]++
switch br.Account { switch br.Account {
case "irc.zzz": case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main-telegram", Account: "irc.zzz", Direction: "inout", ID: "#main-telegramirc.zzz", SameChannel: map[string]bool{"bridge3": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "#main-telegram",
Account: "irc.zzz",
Direction: "inout",
ID: "#main-telegramirc.zzz",
SameChannel: map[string]bool{"bridge3": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
case "telegram.zzz": case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "--333333333333", Account: "telegram.zzz", Direction: "inout", ID: "--333333333333telegram.zzz", SameChannel: map[string]bool{"bridge3": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "--333333333333",
Account: "telegram.zzz",
Direction: "inout",
ID: "--333333333333telegram.zzz",
SameChannel: map[string]bool{"bridge3": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
} }
} }
case "announcements": case "announcements":
@ -265,11 +338,41 @@ func TestGetDestChannelAdvanced(t *testing.T) {
hits[gw.Name]++ hits[gw.Name]++
switch br.Account { switch br.Account {
case "irc.zzz": case "irc.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "#main", Account: "irc.zzz", Direction: "out", ID: "#mainirc.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}, {Name: "#main-help", Account: "irc.zzz", Direction: "out", ID: "#main-helpirc.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Len(t, channels, 2)
assert.Contains(t, channels, config.ChannelInfo{
Name: "#main",
Account: "irc.zzz",
Direction: "out",
ID: "#mainirc.zzz",
SameChannel: map[string]bool{"announcements": false},
Options: config.ChannelOptions{Key: ""},
})
assert.Contains(t, channels, config.ChannelInfo{
Name: "#main-help",
Account: "irc.zzz",
Direction: "out",
ID: "#main-helpirc.zzz",
SameChannel: map[string]bool{"announcements": false},
Options: config.ChannelOptions{Key: ""},
})
case "slack.zzz": case "slack.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "general", Account: "slack.zzz", Direction: "out", ID: "generalslack.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "general",
Account: "slack.zzz",
Direction: "out",
ID: "generalslack.zzz",
SameChannel: map[string]bool{"announcements": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
case "telegram.zzz": case "telegram.zzz":
assert.Equal(t, []config.ChannelInfo{{Name: "--333333333333", Account: "telegram.zzz", Direction: "out", ID: "--333333333333telegram.zzz", SameChannel: map[string]bool{"announcements": false}, Options: config.ChannelOptions{Key: ""}}}, channels) assert.Equal(t, []config.ChannelInfo{{
Name: "--333333333333",
Account: "telegram.zzz",
Direction: "out",
ID: "--333333333333telegram.zzz",
SameChannel: map[string]bool{"announcements": false},
Options: config.ChannelOptions{Key: ""},
}}, channels)
} }
} }
} }