aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/tarm/serial/basic_test.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-27 17:22:56 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-27 17:22:56 +0200
commit3ee2c48a1d5599c4e6754636d1755b1e1e897301 (patch)
treeba857ebb21b0b13248192e1711af496227a2b202 /vendor/github.com/tarm/serial/basic_test.go
parent41acedced077f9da2c6b62a3932d661566af3ca6 (diff)
Add vendor
Diffstat (limited to 'vendor/github.com/tarm/serial/basic_test.go')
-rw-r--r--vendor/github.com/tarm/serial/basic_test.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/github.com/tarm/serial/basic_test.go b/vendor/github.com/tarm/serial/basic_test.go
new file mode 100644
index 0000000..7b9af3e
--- /dev/null
+++ b/vendor/github.com/tarm/serial/basic_test.go
@@ -0,0 +1,69 @@
+// +build linux
+
+package serial
+
+import (
+ "os"
+ "testing"
+ "time"
+)
+
+func TestConnection(t *testing.T) {
+ port0 := os.Getenv("PORT0")
+ port1 := os.Getenv("PORT1")
+ if port0 == "" || port1 == "" {
+ t.Skip("Skipping test because PORT0 or PORT1 environment variable is not set")
+ }
+ c0 := &Config{Name: port0, Baud: 115200}
+ c1 := &Config{Name: port1, Baud: 115200}
+
+ s1, err := OpenPort(c0)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ s2, err := OpenPort(c1)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ ch := make(chan int, 1)
+ go func() {
+ buf := make([]byte, 128)
+ var readCount int
+ for {
+ n, err := s2.Read(buf)
+ if err != nil {
+ t.Fatal(err)
+ }
+ readCount++
+ t.Logf("Read %v %v bytes: % 02x %s", readCount, n, buf[:n], buf[:n])
+ select {
+ case <-ch:
+ ch <- readCount
+ close(ch)
+ default:
+ }
+ }
+ }()
+
+ if _, err = s1.Write([]byte("hello")); err != nil {
+ t.Fatal(err)
+ }
+ if _, err = s1.Write([]byte(" ")); err != nil {
+ t.Fatal(err)
+ }
+ time.Sleep(time.Second)
+ if _, err = s1.Write([]byte("world")); err != nil {
+ t.Fatal(err)
+ }
+ time.Sleep(time.Second / 10)
+
+ ch <- 0
+ s1.Write([]byte(" ")) // We could be blocked in the read without this
+ c := <-ch
+ exp := 5
+ if c >= exp {
+ t.Fatalf("Expected less than %v read, got %v", exp, c)
+ }
+}