2016-10-24 14:16:21 +02:00
|
|
|
package varinterval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-12-29 23:44:45 +01:00
|
|
|
"fmt"
|
2016-10-24 14:16:21 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-23 20:06:52 +02:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/bittorrent"
|
2016-10-24 14:16:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var configTests = []struct {
|
|
|
|
cfg Config
|
|
|
|
expected error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cfg: Config{0.5, 60, true},
|
|
|
|
expected: nil,
|
|
|
|
}, {
|
|
|
|
cfg: Config{1.0, 60, true},
|
|
|
|
expected: nil,
|
|
|
|
}, {
|
|
|
|
cfg: Config{0.0, 60, true},
|
|
|
|
expected: ErrInvalidModifyResponseProbability,
|
|
|
|
}, {
|
|
|
|
cfg: Config{1.1, 60, true},
|
|
|
|
expected: ErrInvalidModifyResponseProbability,
|
|
|
|
}, {
|
|
|
|
cfg: Config{0.5, 0, true},
|
|
|
|
expected: ErrInvalidMaxIncreaseDelta,
|
|
|
|
}, {
|
|
|
|
cfg: Config{0.5, -10, true},
|
|
|
|
expected: ErrInvalidMaxIncreaseDelta,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckConfig(t *testing.T) {
|
2017-12-29 23:44:45 +01:00
|
|
|
for _, tt := range configTests {
|
|
|
|
t.Run(fmt.Sprintf("%#v", tt.cfg), func(t *testing.T) {
|
|
|
|
got := checkConfig(tt.cfg)
|
|
|
|
require.Equal(t, tt.expected, got, "", tt.cfg)
|
2016-10-24 14:16:21 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandleAnnounce(t *testing.T) {
|
2017-12-23 20:54:51 +01:00
|
|
|
h, err := NewHook(Config{1.0, 10, true})
|
2016-10-24 14:16:21 +02:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, h)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
req := &bittorrent.AnnounceRequest{}
|
|
|
|
resp := &bittorrent.AnnounceResponse{}
|
|
|
|
|
|
|
|
nCtx, err := h.HandleAnnounce(ctx, req, resp)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, ctx, nCtx)
|
|
|
|
require.True(t, resp.Interval > 0, "interval should have been increased")
|
|
|
|
require.True(t, resp.MinInterval > 0, "min_interval should have been increased")
|
|
|
|
}
|