2020-04-24 03:02:58 +00:00
|
|
|
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
|
|
|
|
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-09-28 20:19:04 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/utils/ws"
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/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) {
|
2020-12-01 01:49:18 +00:00
|
|
|
config := testenv.Must(t)
|
2020-04-24 03:02:58 +00:00
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
log.Println("The voice channel's name is", c.Name)
|
|
|
|
|
2021-11-04 20:07:18 +00:00
|
|
|
testVoice(t, s, c)
|
|
|
|
|
|
|
|
// BUG: Discord doesn't want to send the second VoiceServerUpdateEvent. I
|
|
|
|
// have no idea why.
|
|
|
|
|
|
|
|
// testVoice(t, s, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testVoice(t *testing.T, s *state.State, c *discord.Channel) {
|
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")
|
|
|
|
})
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
|
|
|
|
t.Cleanup(cancel)
|
|
|
|
|
|
|
|
if err := v.JoinChannel(ctx, c.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() {
|
2020-12-01 01:49:18 +00:00
|
|
|
log.Println("Leaving the voice channel concurrently.")
|
2020-11-17 20:09:15 +00:00
|
|
|
|
2020-12-01 01:49:18 +00:00
|
|
|
raceMe(t, "failed to leave voice channel", func() (interface{}, error) {
|
2021-09-28 20:19:04 +00:00
|
|
|
return nil, 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
|
|
|
|
|
|
|
// Trigger speaking.
|
2021-09-28 20:19:04 +00:00
|
|
|
if err := v.Speaking(ctx, 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-11-17 20:09:15 +00:00
|
|
|
f, err := os.Open("testdata/nico.dca")
|
|
|
|
if err != nil {
|
2021-11-04 20:07:18 +00:00
|
|
|
t.Fatal("failed to open nico.dca:", err)
|
2020-11-17 20:09:15 +00:00
|
|
|
}
|
|
|
|
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.
|
2020-12-01 01:49:18 +00:00
|
|
|
if _, err := io.CopyN(v, f, framelen); err != nil && err != io.EOF {
|
2020-11-17 20:09:15 +00:00
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|