Make private announce test more complete and fix brittle peer order

This commit is contained in:
Justin Li
2014-07-15 22:44:20 -04:00
parent 9adf9a114c
commit 5ccb42a0c7
3 changed files with 60 additions and 29 deletions

View File

@@ -9,7 +9,9 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"sort"
"github.com/chihaya/bencode"
"github.com/chihaya/chihaya/config"
_ "github.com/chihaya/chihaya/drivers/backend/noop"
@@ -47,3 +49,28 @@ func announce(p params, srv *httptest.Server) ([]byte, error) {
response.Body.Close()
return body, err
}
type peerList bencode.List
func (p peerList) Len() int {
return len(p)
}
func (p peerList) Less(i, j int) bool {
if peer1, ok := p[i].(bencode.Dict); ok {
if peer2, ok := p[j].(bencode.Dict); ok {
return peer1["peer id"].(string) < peer2["peer id"].(string)
}
}
return false
}
func (p peerList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func sortPeersInResponse(dict bencode.Dict) {
if peers, ok := dict["peers"].(bencode.List); ok {
sort.Stable(peerList(peers))
}
}