2021-03-26 23:52:07 +01:00
|
|
|
// Copyright 2021 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-03-27 00:49:16 +01:00
|
|
|
"context"
|
2021-03-26 23:52:07 +01:00
|
|
|
"reflect"
|
2021-03-27 00:49:16 +01:00
|
|
|
"sort"
|
2021-03-26 23:52:07 +01:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
irc "github.com/fluffle/goirc/client"
|
|
|
|
)
|
|
|
|
|
2021-03-27 00:15:35 +01:00
|
|
|
func makeReconcilerTestIRCConfig(IRCPort int) *Config {
|
|
|
|
config := makeTestIRCConfig(IRCPort)
|
|
|
|
config.IRCChannels = []IRCChannel{
|
|
|
|
IRCChannel{Name: "#foo"},
|
|
|
|
IRCChannel{Name: "#bar"},
|
|
|
|
IRCChannel{Name: "#baz"},
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-03-26 23:52:07 +01:00
|
|
|
func makeTestReconciler(config *Config) (*ChannelReconciler, chan bool, chan bool) {
|
|
|
|
|
|
|
|
sessionUp := make(chan bool)
|
|
|
|
sessionDown := make(chan bool)
|
|
|
|
|
|
|
|
client := irc.Client(makeGOIRCConfig(config))
|
|
|
|
client.Config().Flood = true
|
|
|
|
client.HandleFunc(irc.CONNECTED,
|
|
|
|
func(*irc.Conn, *irc.Line) {
|
|
|
|
sessionUp <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
client.HandleFunc(irc.DISCONNECTED,
|
|
|
|
func(*irc.Conn, *irc.Line) {
|
|
|
|
sessionDown <- false
|
|
|
|
})
|
|
|
|
|
|
|
|
fakeDelayerMaker := &FakeDelayerMaker{}
|
|
|
|
reconciler := NewChannelReconciler(config, client, fakeDelayerMaker)
|
|
|
|
|
|
|
|
return reconciler, sessionUp, sessionDown
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreJoinChannels(t *testing.T) {
|
|
|
|
server, port := makeTestServer(t)
|
2021-03-27 00:15:35 +01:00
|
|
|
config := makeReconcilerTestIRCConfig(port)
|
2021-03-26 23:52:07 +01:00
|
|
|
reconciler, sessionUp, sessionDown := makeTestReconciler(config)
|
|
|
|
|
|
|
|
var testStep sync.WaitGroup
|
|
|
|
|
2021-03-27 00:49:16 +01:00
|
|
|
joinedChannels := []string{}
|
|
|
|
|
2021-03-26 23:52:07 +01:00
|
|
|
joinHandler := func(conn *bufio.ReadWriter, line *irc.Line) error {
|
2021-03-27 00:49:16 +01:00
|
|
|
joinedChannels = append(joinedChannels, line.Args[0])
|
|
|
|
if len(joinedChannels) == 3 {
|
2021-03-26 23:52:07 +01:00
|
|
|
testStep.Done()
|
|
|
|
}
|
2021-03-27 00:49:16 +01:00
|
|
|
return hJOIN(conn, line)
|
2021-03-26 23:52:07 +01:00
|
|
|
}
|
|
|
|
server.SetHandler("JOIN", joinHandler)
|
|
|
|
|
|
|
|
testStep.Add(1)
|
|
|
|
|
|
|
|
reconciler.client.Connect()
|
|
|
|
|
|
|
|
<-sessionUp
|
2021-03-27 00:49:16 +01:00
|
|
|
reconciler.Start(context.Background())
|
2021-03-26 23:52:07 +01:00
|
|
|
|
|
|
|
testStep.Wait()
|
|
|
|
|
|
|
|
reconciler.client.Quit("see ya")
|
|
|
|
<-sessionDown
|
2021-03-27 00:49:16 +01:00
|
|
|
reconciler.Stop()
|
2021-03-26 23:52:07 +01:00
|
|
|
|
|
|
|
server.Stop()
|
|
|
|
|
2021-03-27 00:49:16 +01:00
|
|
|
expectedJoinedChannels := []string{"#bar", "#baz", "#foo"}
|
|
|
|
sort.Strings(joinedChannels)
|
2021-03-26 23:52:07 +01:00
|
|
|
|
2021-03-27 00:49:16 +01:00
|
|
|
if !reflect.DeepEqual(expectedJoinedChannels, joinedChannels) {
|
2021-03-26 23:52:07 +01:00
|
|
|
t.Error("Did not pre-join channels")
|
|
|
|
}
|
|
|
|
}
|