2020-04-24 03:02:58 +00:00
|
|
|
package voice
|
|
|
|
|
|
|
|
import (
|
2020-10-22 17:47:27 +00:00
|
|
|
"context"
|
2023-09-19 15:25:48 +00:00
|
|
|
"errors"
|
2023-09-19 15:23:25 +00:00
|
|
|
"fmt"
|
2020-04-24 03:02:58 +00:00
|
|
|
"log"
|
2021-07-02 09:42:00 +00:00
|
|
|
"math/rand"
|
2020-04-24 03:02:58 +00:00
|
|
|
"os"
|
2020-04-25 02:36:33 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2020-11-17 20:09:15 +00:00
|
|
|
"sync"
|
2020-04-24 03:02:58 +00:00
|
|
|
"testing"
|
2020-04-25 02:36:33 +00:00
|
|
|
"time"
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/api"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v3/internal/testenv"
|
|
|
|
"github.com/diamondburned/arikawa/v3/state"
|
2021-07-02 09:42:00 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/json/option"
|
2021-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/ws"
|
2021-07-02 09:42:00 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/voice/testdata"
|
|
|
|
"github.com/diamondburned/arikawa/v3/voice/udp"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/voice/voicegateway"
|
2020-04-24 03:02:58 +00:00
|
|
|
)
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2021-09-28 20:19:04 +00:00
|
|
|
ws.WSDebug = func(v ...interface{}) {
|
2020-04-25 02:36:33 +00:00
|
|
|
_, file, line, _ := runtime.Caller(1)
|
|
|
|
caller := file + ":" + strconv.Itoa(line)
|
|
|
|
log.Println(append([]interface{}{caller}, v...)...)
|
2020-04-24 03:02:58 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
code := m.Run()
|
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testState struct {
|
|
|
|
*state.State
|
|
|
|
channel *discord.Channel
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOpen(t *testing.T) *testState {
|
|
|
|
config := testenv.Must(t)
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
s := state.New("Bot " + config.BotToken)
|
|
|
|
AddIntents(s)
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2021-06-10 23:48:32 +00:00
|
|
|
func() {
|
2021-11-03 22:16:02 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2021-06-10 23:48:32 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := s.Open(ctx); err != nil {
|
2021-11-04 20:07:18 +00:00
|
|
|
t.Fatal("failed to connect:", err)
|
2021-06-10 23:48:32 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
t.Cleanup(func() { s.Close() })
|
2020-04-24 03:02:58 +00:00
|
|
|
|
|
|
|
// Validate the given voice channel.
|
2020-12-01 01:49:18 +00:00
|
|
|
c, err := s.Channel(config.VoiceChID)
|
2020-04-24 03:02:58 +00:00
|
|
|
if err != nil {
|
2021-11-04 20:07:18 +00:00
|
|
|
t.Fatal("failed to get channel:", err)
|
2020-04-24 03:02:58 +00:00
|
|
|
}
|
|
|
|
if c.Type != discord.GuildVoice {
|
2021-11-04 20:07:18 +00:00
|
|
|
t.Fatal("channel isn't a guild voice channel.")
|
2020-04-24 03:02:58 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
t.Log("The voice channel's name is", c.Name)
|
2020-07-11 19:50:32 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
return &testState{
|
|
|
|
State: s,
|
|
|
|
channel: c,
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 20:07:18 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
func TestIntegration(t *testing.T) {
|
|
|
|
state := testOpen(t)
|
2021-11-04 20:07:18 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
t.Run("1st", func(t *testing.T) { testIntegrationOnce(t, state) })
|
|
|
|
t.Run("2nd", func(t *testing.T) { testIntegrationOnce(t, state) })
|
2021-11-04 20:07:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
func testIntegrationOnce(t *testing.T, s *testState) {
|
2020-12-01 01:49:18 +00:00
|
|
|
v, err := NewSession(s)
|
|
|
|
if err != nil {
|
2021-11-04 20:07:18 +00:00
|
|
|
t.Fatal("failed to create a new voice session:", err)
|
2020-12-01 01:49:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Grab a timer to benchmark things.
|
|
|
|
finish := timer()
|
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
// Add handler to receive speaking update beforehand.
|
|
|
|
v.AddHandler(func(e *voicegateway.SpeakingEvent) {
|
|
|
|
finish("receiving voice speaking event")
|
|
|
|
})
|
|
|
|
|
2022-04-03 05:42:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2021-09-28 20:19:04 +00:00
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
if err := v.JoinChannelAndSpeak(ctx, s.channel.ID, false, false); err != nil {
|
2021-11-04 20:07:18 +00:00
|
|
|
t.Fatal("failed to join voice:", err)
|
|
|
|
}
|
2020-11-17 20:09:15 +00:00
|
|
|
|
|
|
|
t.Cleanup(func() {
|
2021-07-02 09:42:00 +00:00
|
|
|
t.Log("Leaving the voice channel concurrently.")
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
raceMe(t, "failed to leave voice channel", func() error {
|
|
|
|
return v.Leave(ctx)
|
2020-11-17 20:09:15 +00:00
|
|
|
})
|
|
|
|
})
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
finish("joining the voice channel")
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
t.Cleanup(func() {})
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
finish("sending the speaking command")
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
if err := testdata.WriteOpus(v, testdata.Nico); err != nil {
|
|
|
|
t.Error(err)
|
2020-11-17 20:09:15 +00:00
|
|
|
}
|
2021-07-02 09:42:00 +00:00
|
|
|
doneCh <- struct{}{}
|
|
|
|
}()
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
t.Error("timed out waiting for voice to be done")
|
|
|
|
case <-doneCh:
|
|
|
|
finish("copying the audio")
|
2020-11-17 20:09:15 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 20:09:15 +00:00
|
|
|
// raceMe intentionally calls fn multiple times in goroutines to ensure it's not
|
|
|
|
// racy.
|
2021-07-02 09:42:00 +00:00
|
|
|
func raceMe(t *testing.T, wrapErr string, fn func() error) {
|
2020-11-17 20:09:15 +00:00
|
|
|
const n = 3 // run 3 times
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// It is very ironic how this method itself is racy.
|
|
|
|
|
|
|
|
var wgr sync.WaitGroup
|
|
|
|
var mut sync.Mutex
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
wgr.Add(1)
|
|
|
|
go func() {
|
2021-07-02 09:42:00 +00:00
|
|
|
e := fn()
|
2020-11-17 20:09:15 +00:00
|
|
|
|
|
|
|
mut.Lock()
|
2021-07-02 09:42:00 +00:00
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
2020-11-17 20:09:15 +00:00
|
|
|
mut.Unlock()
|
|
|
|
|
|
|
|
if e != nil {
|
2021-07-02 09:42:00 +00:00
|
|
|
t.Log("Potential race test error:", e)
|
2020-11-17 20:09:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wgr.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wgr.Wait()
|
|
|
|
|
|
|
|
if err != nil {
|
2023-09-19 15:23:25 +00:00
|
|
|
t.Fatal("race test failed:", fmt.Errorf("%s: %w", wrapErr, err))
|
2020-11-17 20:09:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// simple shitty benchmark thing
|
|
|
|
func timer() func(finished string) {
|
|
|
|
var then = time.Now()
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
return func(finished string) {
|
|
|
|
now := time.Now()
|
|
|
|
log.Println("Finished", finished+", took", now.Sub(then))
|
|
|
|
then = now
|
2020-04-24 03:02:58 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-02 09:42:00 +00:00
|
|
|
|
|
|
|
func TestKickedOut(t *testing.T) {
|
|
|
|
err := testReconnect(t, func(s *testState) {
|
|
|
|
me, err := s.Me()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cannot get me")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.ModifyMember(s.channel.GuildID, me.ID, api.ModifyMemberData{
|
|
|
|
// Kick the bot out.
|
|
|
|
VoiceChannel: discord.NullChannelID,
|
|
|
|
}); err != nil {
|
|
|
|
t.Error("cannot kick the bot out:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if !errors.Is(err, udp.ErrManagerClosed) {
|
|
|
|
t.Error("unexpected error while sending nico.dca:", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRegionChange(t *testing.T) {
|
|
|
|
var state *testState
|
|
|
|
err := testReconnect(t, func(s *testState) {
|
|
|
|
state = s
|
|
|
|
t.Log("got voice region", s.channel.RTCRegionID)
|
|
|
|
|
|
|
|
regions, err := s.VoiceRegionsGuild(s.channel.GuildID)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("cannot get voice region:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rand.Shuffle(len(regions), func(i, j int) {
|
|
|
|
regions[i], regions[j] = regions[j], regions[i]
|
|
|
|
})
|
|
|
|
|
|
|
|
var anyRegion string
|
|
|
|
for _, region := range regions {
|
|
|
|
if region.ID != s.channel.RTCRegionID {
|
|
|
|
anyRegion = region.ID
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("changing voice region to", anyRegion)
|
|
|
|
|
|
|
|
if err := s.ModifyChannel(s.channel.ID, api.ModifyChannelData{
|
|
|
|
RTCRegionID: option.NewNullableString(anyRegion),
|
|
|
|
}); err != nil {
|
|
|
|
t.Error("cannot change voice region:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error("unexpected error while sending nico.dca:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := state
|
|
|
|
|
|
|
|
// Change voice region back.
|
|
|
|
if err := s.ModifyChannel(s.channel.ID, api.ModifyChannelData{
|
|
|
|
RTCRegionID: option.NewNullableString(s.channel.RTCRegionID),
|
|
|
|
}); err != nil {
|
|
|
|
t.Error("cannot change voice region back:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("changed voice region back to", s.channel.RTCRegionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testReconnect(t *testing.T, interrupt func(*testState)) error {
|
|
|
|
s := testOpen(t)
|
|
|
|
|
|
|
|
v, err := NewSession(s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cannot")
|
|
|
|
}
|
|
|
|
|
2022-04-03 05:42:39 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
2021-07-02 09:42:00 +00:00
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
if err := v.JoinChannelAndSpeak(ctx, s.channel.ID, false, false); err != nil {
|
|
|
|
t.Fatal("failed to join voice:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := v.Speaking(ctx, voicegateway.NotSpeaking); err != nil {
|
|
|
|
t.Error("cannot stop speaking:", err)
|
|
|
|
}
|
|
|
|
if err := v.Leave(ctx); err != nil {
|
|
|
|
t.Error("cannot leave voice:", err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Ensure the channel is buffered so we can send into it. Write may not be
|
|
|
|
// called often enough to immediately receive a tick from the unbuffered
|
|
|
|
// timer.
|
|
|
|
oneSec := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
|
|
<-time.After(450 * time.Millisecond)
|
|
|
|
oneSec <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Use a WriterFunc so we can interrupt the writing.
|
|
|
|
// Give 1s for the function to write before interrupting it; we already know
|
|
|
|
// that the saved dca file is longer than 1s, so we're fine doing this.
|
|
|
|
interruptWriter := testdata.WriterFunc(func(b []byte) (int, error) {
|
|
|
|
select {
|
|
|
|
case <-oneSec:
|
|
|
|
interrupt(s)
|
|
|
|
default:
|
|
|
|
// ok
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.Write(b)
|
|
|
|
})
|
|
|
|
|
|
|
|
return testdata.WriteOpus(interruptWriter, testdata.Nico)
|
|
|
|
}
|