mirror of
https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/snowflake.git
synced 2025-10-14 05:11:19 -04:00
Convert Broker SnowflakeHeap test to goconvey, and async test for client handler
This commit is contained in:
parent
b04d1f67fb
commit
0e1c5a1756
2 changed files with 120 additions and 78 deletions
|
@ -1,67 +1,115 @@
|
|||
package snowflake_broker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"container/heap"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSnowflakeHeap(t *testing.T) {
|
||||
h := new(SnowflakeHeap)
|
||||
heap.Init(h)
|
||||
if 0 != h.Len() {
|
||||
t.Error("Unexpected length.")
|
||||
}
|
||||
s1 := new(Snowflake)
|
||||
s2 := new(Snowflake)
|
||||
s3 := new(Snowflake)
|
||||
s4 := new(Snowflake)
|
||||
func TestBroker(t *testing.T) {
|
||||
|
||||
s1.clients = 4
|
||||
s2.clients = 5
|
||||
s3.clients = 3
|
||||
s4.clients = 1
|
||||
Convey("Context", t, func() {
|
||||
ctx := NewBrokerContext()
|
||||
|
||||
heap.Push(h, s1)
|
||||
if 1 != h.Len() {
|
||||
}
|
||||
heap.Push(h, s2)
|
||||
heap.Push(h, s3)
|
||||
heap.Push(h, s4)
|
||||
Convey("Adds Snowflake", func() {
|
||||
ctx := NewBrokerContext()
|
||||
So(ctx.snowflakes.Len(), ShouldEqual, 0)
|
||||
So(len(ctx.snowflakeMap), ShouldEqual, 0)
|
||||
ctx.AddSnowflake("foo")
|
||||
So(ctx.snowflakes.Len(), ShouldEqual, 1)
|
||||
So(len(ctx.snowflakeMap), ShouldEqual, 1)
|
||||
})
|
||||
|
||||
if 4 != h.Len() {
|
||||
t.Error("Unexpected length.")
|
||||
}
|
||||
Convey("Responds to client offers...", func() {
|
||||
|
||||
heap.Remove(h, 0)
|
||||
if 3 != h.Len() {
|
||||
t.Error("Unexpected length.")
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
data := bytes.NewReader([]byte("test"))
|
||||
r, err := http.NewRequest("POST", "broker.com/client", data)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
r := heap.Pop(h).(*Snowflake)
|
||||
if 2 != h.Len() {
|
||||
t.Error("Unexpected length.")
|
||||
}
|
||||
if r.clients != 3 {
|
||||
t.Error("Unexpected clients: ", r.clients)
|
||||
}
|
||||
if r.index != -1 {
|
||||
t.Error("Unexpected index: ", r.index)
|
||||
}
|
||||
Convey("with 503 when no snowflakes are available.", func() {
|
||||
clientHandler(ctx, w, r)
|
||||
h := w.Header()
|
||||
So(h["Access-Control-Allow-Headers"], ShouldNotBeNil)
|
||||
So(w.Code, ShouldEqual, http.StatusServiceUnavailable)
|
||||
So(w.Body.String(), ShouldEqual, "")
|
||||
})
|
||||
|
||||
r = heap.Pop(h).(*Snowflake)
|
||||
if 1 != h.Len() {
|
||||
t.Error("Unexpected length.")
|
||||
}
|
||||
if r.clients != 4 {
|
||||
t.Error("Unexpected clients: ", r.clients)
|
||||
}
|
||||
Convey("with a proxy answer if available.", func() {
|
||||
done := make(chan bool)
|
||||
// Prepare a fake proxy to respond with.
|
||||
snowflake := ctx.AddSnowflake("fake")
|
||||
go func() {
|
||||
clientHandler(ctx, w, r)
|
||||
done <- true
|
||||
}()
|
||||
offer := <-snowflake.offerChannel
|
||||
So(offer, ShouldResemble, []byte("test"))
|
||||
snowflake.answerChannel <- []byte("fake answer")
|
||||
<-done
|
||||
So(w.Body.String(), ShouldEqual, "fake answer")
|
||||
So(w.Code, ShouldEqual, http.StatusOK)
|
||||
})
|
||||
|
||||
r = heap.Pop(h).(*Snowflake)
|
||||
if r.clients != 5 {
|
||||
t.Error("Unexpected clients: ", r.clients)
|
||||
}
|
||||
Convey("Times out when no proxy responds.", func() {
|
||||
done := make(chan bool)
|
||||
snowflake := ctx.AddSnowflake("fake")
|
||||
go func() {
|
||||
clientHandler(ctx, w, r)
|
||||
done <- true
|
||||
}()
|
||||
offer := <-snowflake.offerChannel
|
||||
So(offer, ShouldResemble, []byte("test"))
|
||||
<-done
|
||||
So(w.Code, ShouldEqual, http.StatusGatewayTimeout)
|
||||
})
|
||||
|
||||
if 0 != h.Len() {
|
||||
t.Error("Unexpected length.")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSnowflakeHeap(t *testing.T) {
|
||||
Convey("SnowflakeHeap", t, func() {
|
||||
h := new(SnowflakeHeap)
|
||||
heap.Init(h)
|
||||
So(h.Len(), ShouldEqual, 0)
|
||||
s1 := new(Snowflake)
|
||||
s2 := new(Snowflake)
|
||||
s3 := new(Snowflake)
|
||||
s4 := new(Snowflake)
|
||||
s1.clients = 4
|
||||
s2.clients = 5
|
||||
s3.clients = 3
|
||||
s4.clients = 1
|
||||
|
||||
heap.Push(h, s1)
|
||||
So(h.Len(), ShouldEqual, 1)
|
||||
heap.Push(h, s2)
|
||||
So(h.Len(), ShouldEqual, 2)
|
||||
heap.Push(h, s3)
|
||||
So(h.Len(), ShouldEqual, 3)
|
||||
heap.Push(h, s4)
|
||||
So(h.Len(), ShouldEqual, 4)
|
||||
|
||||
heap.Remove(h, 0)
|
||||
So(h.Len(), ShouldEqual, 3)
|
||||
|
||||
r := heap.Pop(h).(*Snowflake)
|
||||
So(h.Len(), ShouldEqual, 2)
|
||||
So(r.clients, ShouldEqual, 3)
|
||||
So(r.index, ShouldEqual, -1)
|
||||
|
||||
r = heap.Pop(h).(*Snowflake)
|
||||
So(h.Len(), ShouldEqual, 1)
|
||||
So(r.clients, ShouldEqual, 4)
|
||||
So(r.index, ShouldEqual, -1)
|
||||
|
||||
r = heap.Pop(h).(*Snowflake)
|
||||
So(h.Len(), ShouldEqual, 0)
|
||||
So(r.clients, ShouldEqual, 5)
|
||||
So(r.index, ShouldEqual, -1)
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue