-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_test.go
More file actions
89 lines (83 loc) · 2.23 KB
/
client_test.go
File metadata and controls
89 lines (83 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package golibbuttplug
import (
"context"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/funjack/golibbuttplug/buttplugtest"
)
func makeWsProto(s string) string {
return "ws" + strings.TrimPrefix(s, "http")
}
// TestButtplugClient only tests if there are no errors when talking with a
// (fake) buttplug server.
func TestButtplugClient(t *testing.T) {
s := buttplugtest.DefaultTestServer
http.Handle("/", s)
ts := httptest.NewServer(s)
defer ts.Close()
// Contexts can be used to cancel client connection.
rootctx := context.Background()
// Create a new session with the server as "ExampleClient".
c, err := NewClient(rootctx, makeWsProto(ts.URL), "ExampleClient", nil)
if err != nil {
log.Fatal(err)
}
// Scan for devices.
if err := c.StartScanning(); err != nil {
log.Fatal(err)
}
// Simulate some events.
go func() {
time.Sleep(100 * time.Millisecond)
s.Conn.SendScanningFinished()
time.Sleep(10 * time.Millisecond)
s.Conn.AddDevice(buttplugtest.DefaultAddDeviceMessage)
time.Sleep(10 * time.Millisecond)
s.Conn.RemoveDevice(buttplugtest.DefaultAddDeviceMessage)
}()
// Wait for scanning to finish.
ctx, cancel := context.WithTimeout(rootctx, 30*time.Second)
err = c.WaitOnScanning(ctx)
cancel()
if err == context.DeadlineExceeded {
// Stop scanning.
if err := c.StopScanning(); err != nil {
log.Fatal(err)
}
} else if err != nil {
log.Fatal(err)
}
// Get all known devices.
log.Printf("devices: %v", c.Devices())
for _, d := range c.Devices() {
go HandleDisconnect(d)
// Test if the RawCmd is supported by the device.
if d.IsSupported("RawCmd") {
log.Printf("%s supports RawCmd", d.Name())
// Send a RawCmd.
if err := d.RawCmd([]byte{0x00, 0x10}); err != nil {
log.Printf("RawCmd failed: %v", err)
}
}
// Try sending a Fleshlight Launch command.
if err := d.FleshlightLaunchFW12Cmd(50, 20); err != nil {
log.Printf("Launch command failed: %v", err)
}
}
time.Sleep(200 * time.Millisecond)
// Stop all devices.
if err := c.StopAllDevices(); err != nil {
log.Fatal(err)
}
// Close the connection.
c.Close()
time.Sleep(100 * time.Millisecond)
}
func HandleDisconnect(d *Device) {
<-d.Disconnected()
log.Printf("Lost device: %s", d.Name())
}