Armon Dadgar 10 лет назад
Родитель
Сommit
eb6b77436e
2 измененных файлов с 66 добавлено и 0 удалено
  1. 16 0
      util.go
  2. 50 0
      util_test.go

+ 16 - 0
util.go

@@ -10,3 +10,19 @@ func asyncSendErr(ch chan error, err error) {
 	default:
 	}
 }
+
+// asyncNotify is used to signal a waiting goroutine
+func asyncNotify(ch chan struct{}) {
+	select {
+	case ch <- struct{}{}:
+	default:
+	}
+}
+
+// min computes the minimum of two values
+func min(a, b uint32) uint32 {
+	if a < b {
+		return a
+	}
+	return b
+}

+ 50 - 0
util_test.go

@@ -0,0 +1,50 @@
+package yamux
+
+import (
+	"testing"
+)
+
+func TestAsyncSendErr(t *testing.T) {
+	ch := make(chan error)
+	asyncSendErr(ch, ErrTimeout)
+	select {
+	case <-ch:
+		t.Fatalf("should not get")
+	default:
+	}
+
+	ch = make(chan error, 1)
+	asyncSendErr(ch, ErrTimeout)
+	select {
+	case <-ch:
+	default:
+		t.Fatalf("should get")
+	}
+}
+
+func TestAsyncNotify(t *testing.T) {
+	ch := make(chan struct{})
+	asyncNotify(ch)
+	select {
+	case <-ch:
+		t.Fatalf("should not get")
+	default:
+	}
+
+	ch = make(chan struct{}, 1)
+	asyncNotify(ch)
+	select {
+	case <-ch:
+	default:
+		t.Fatalf("should get")
+	}
+}
+
+func TestMin(t *testing.T) {
+	if min(1, 2) != 1 {
+		t.Fatalf("bad")
+	}
+	if min(2, 1) != 1 {
+		t.Fatalf("bad")
+	}
+}