2020-04-24 03:02:58 +00:00
|
|
|
// +build integration
|
|
|
|
|
|
|
|
package voice
|
|
|
|
|
|
|
|
import (
|
2020-10-22 17:47:27 +00:00
|
|
|
"context"
|
2020-04-24 03:02:58 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"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
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
|
|
|
"github.com/diamondburned/arikawa/v2/gateway"
|
|
|
|
"github.com/diamondburned/arikawa/v2/utils/wsutil"
|
|
|
|
"github.com/diamondburned/arikawa/v2/voice/voicegateway"
|
2020-11-17 20:09:15 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-04-24 03:02:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntegration(t *testing.T) {
|
|
|
|
config := mustConfig(t)
|
|
|
|
|
2020-04-25 02:36:33 +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
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2020-12-01 00:46:43 +00:00
|
|
|
v, err := NewFromToken("Bot " + config.BotToken)
|
2020-04-24 03:02:58 +00:00
|
|
|
if err != nil {
|
2020-07-11 19:50:32 +00:00
|
|
|
t.Fatal("Failed to create a new voice session:", err)
|
2020-04-24 03:02:58 +00:00
|
|
|
}
|
2020-10-29 20:27:32 +00:00
|
|
|
v.Gateway.AddIntents(gateway.IntentGuildVoiceStates)
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
v.ErrorLog = func(err error) {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
if err := v.Open(); err != nil {
|
2020-04-24 03:02:58 +00:00
|
|
|
t.Fatal("Failed to connect:", err)
|
|
|
|
}
|
2020-11-17 20:09:15 +00:00
|
|
|
t.Cleanup(func() { v.Close() })
|
2020-04-24 03:02:58 +00:00
|
|
|
|
|
|
|
// Validate the given voice channel.
|
2020-07-11 19:50:32 +00:00
|
|
|
c, err := v.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.")
|
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
log.Println("The voice channel's name is", c.Name)
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Grab a timer to benchmark things.
|
|
|
|
finish := timer()
|
|
|
|
|
2020-11-17 20:09:15 +00:00
|
|
|
// Join the voice channel concurrently.
|
|
|
|
raceValue := raceMe(t, "failed to join voice channel", func() (interface{}, error) {
|
2020-12-01 01:04:11 +00:00
|
|
|
return v.JoinChannel(c.ID, false, false)
|
2020-11-17 20:09:15 +00:00
|
|
|
})
|
|
|
|
vs := raceValue.(*Session)
|
|
|
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
log.Println("Disconnecting from the voice channel concurrently.")
|
|
|
|
|
|
|
|
raceMe(t, "failed to disconnect", func() (interface{}, error) {
|
|
|
|
return nil, vs.Disconnect()
|
|
|
|
})
|
|
|
|
})
|
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
|
|
|
|
2020-11-15 19:45:47 +00:00
|
|
|
// Add handler to receive speaking update
|
|
|
|
vs.AddHandler(func(e *voicegateway.SpeakingEvent) {
|
|
|
|
finish("received voice speaking event")
|
|
|
|
})
|
|
|
|
|
2020-11-17 20:09:15 +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.
|
2020-04-25 02:36:33 +00:00
|
|
|
if err := vs.Speaking(voicegateway.Microphone); err != nil {
|
2020-11-17 20:09:15 +00:00
|
|
|
t.Fatal("failed to start speaking:", err)
|
2020-04-24 03:02:58 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
finish("sending the speaking command")
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
if err := vs.UseContext(ctx); err != nil {
|
|
|
|
t.Fatal("failed to set ctx into vs:", err)
|
|
|
|
}
|
|
|
|
|
2020-11-17 20:09:15 +00:00
|
|
|
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?
|
2020-11-17 20:09:15 +00:00
|
|
|
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(vs, f, framelen); err != nil && err != io.EOF {
|
|
|
|
t.Fatal("failed to write:", err)
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 03:02:58 +00:00
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
finish("copying the audio")
|
|
|
|
}
|
|
|
|
|
2020-11-17 20:09:15 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
type testConfig struct {
|
|
|
|
BotToken string
|
|
|
|
VoiceChID discord.ChannelID
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustConfig(t *testing.T) testConfig {
|
|
|
|
var token = os.Getenv("BOT_TOKEN")
|
|
|
|
if token == "" {
|
|
|
|
t.Fatal("Missing $BOT_TOKEN")
|
|
|
|
}
|
|
|
|
|
|
|
|
var sid = os.Getenv("VOICE_ID")
|
|
|
|
if sid == "" {
|
|
|
|
t.Fatal("Missing $VOICE_ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := discord.ParseSnowflake(sid)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Invalid $VOICE_ID:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return testConfig{
|
|
|
|
BotToken: token,
|
|
|
|
VoiceChID: discord.ChannelID(id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// file is only a few bytes lolmao
|
|
|
|
func nicoReadTo(t *testing.T, dst io.Writer) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|