mirror of
https://github.com/sot-tech/mochi.git
synced 2026-05-11 06:39:08 -07:00
Make private announce test more complete and fix brittle peer order
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user