1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-09 16:35:12 +00:00
arikawa/voice/session_test.go

178 lines
3.8 KiB
Go
Raw Normal View History

2020-04-24 03:02:58 +00:00
package voice
import (
"context"
2020-04-24 03:02:58 +00:00
"encoding/binary"
"io"
"log"
"os"
"runtime"
"strconv"
"sync"
2020-04-24 03:02:58 +00:00
"testing"
"time"
2020-04-24 03:02:58 +00:00
2020-10-28 22:39:59 +00:00
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/internal/testenv"
"github.com/diamondburned/arikawa/v2/state"
2020-10-28 22:39:59 +00:00
"github.com/diamondburned/arikawa/v2/utils/wsutil"
"github.com/diamondburned/arikawa/v2/voice/voicegateway"
"github.com/pkg/errors"
2020-04-24 03:02:58 +00:00
)
func TestIntegration(t *testing.T) {
config := testenv.Must(t)
2020-04-24 03:02:58 +00:00
wsutil.WSDebug = func(v ...interface{}) {
_, file, line, _ := runtime.Caller(1)
caller := file + ":" + strconv.Itoa(line)
log.Println(append([]interface{}{caller}, v...)...)
2020-04-24 03:02:58 +00:00
}
s, err := state.New("Bot " + config.BotToken)
2020-04-24 03:02:58 +00:00
if err != nil {
t.Fatal("Failed to create a new state:", err)
}
AddIntents(s.Gateway)
2020-04-24 03:02:58 +00:00
if err := s.Open(); err != nil {
2020-04-24 03:02:58 +00:00
t.Fatal("Failed to connect:", err)
}
t.Cleanup(func() { s.Close() })
2020-04-24 03:02:58 +00:00
// Validate the given voice channel.
c, err := s.Channel(config.VoiceChID)
2020-04-24 03:02:58 +00:00
if err != nil {
t.Fatal("Failed to get channel:", err)
}
if c.Type != discord.GuildVoice {
t.Fatal("Channel isn't a guild voice channel.")
}
log.Println("The voice channel's name is", c.Name)
v, err := NewSession(s)
if err != nil {
t.Fatal("Failed to create a new voice session:", err)
}
v.ErrorLog = func(err error) { t.Error(err) }
// Grab a timer to benchmark things.
finish := timer()
// Add handler to receive speaking update beforehand.
v.AddHandler(func(e *voicegateway.SpeakingEvent) {
finish("receiving voice speaking event")
})
// Join the voice channel concurrently.
raceMe(t, "failed to join voice channel", func() (interface{}, error) {
return nil, v.JoinChannel(c.GuildID, c.ID, false, false)
})
t.Cleanup(func() {
log.Println("Leaving the voice channel concurrently.")
raceMe(t, "failed to leave voice channel", func() (interface{}, error) {
return nil, v.Leave()
})
})
2020-04-24 03:02:58 +00:00
finish("joining the voice channel")
2020-04-24 03:02:58 +00:00
// Create a context and only cancel it AFTER we're done sending silence
// frames.
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
t.Cleanup(cancel)
2020-04-24 03:02:58 +00:00
// Trigger speaking.
if err := v.Speaking(voicegateway.Microphone); err != nil {
t.Fatal("failed to start speaking:", err)
2020-04-24 03:02:58 +00:00
}
finish("sending the speaking command")
2020-04-24 03:02:58 +00:00
if err := v.UseContext(ctx); err != nil {
t.Fatal("failed to set ctx into vs:", err)
}
f, err := os.Open("testdata/nico.dca")
if err != nil {
t.Fatal("Failed to open nico.dca:", err)
}
defer f.Close()
var lenbuf [4]byte
2020-04-24 03:02:58 +00:00
// Copy the audio?
for {
if _, err := io.ReadFull(f, lenbuf[:]); err != nil {
if err == io.EOF {
break
}
t.Fatal("failed to read:", err)
}
// Read the integer
framelen := int64(binary.LittleEndian.Uint32(lenbuf[:]))
// Copy the frame.
if _, err := io.CopyN(v, f, framelen); err != nil && err != io.EOF {
t.Fatal("failed to write:", err)
}
}
2020-04-24 03:02:58 +00:00
finish("copying the audio")
}
// raceMe intentionally calls fn multiple times in goroutines to ensure it's not
// racy.
func raceMe(t *testing.T, wrapErr string, fn func() (interface{}, error)) interface{} {
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 val interface{}
var err error
for i := 0; i < n; i++ {
wgr.Add(1)
go func() {
v, e := fn()
mut.Lock()
val = v
err = e
mut.Unlock()
if e != nil {
log.Println("Potential race test error:", e)
}
wgr.Done()
}()
}
wgr.Wait()
if err != nil {
t.Fatal("Race test failed:", errors.Wrap(err, wrapErr))
}
return val
}
// simple shitty benchmark thing
func timer() func(finished string) {
var then = time.Now()
2020-04-24 03:02:58 +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
}
}