etcd/rafthttp/transport_test.go

89 lines
2.2 KiB
Go
Raw Normal View History

// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-11-07 03:48:03 +03:00
package rafthttp
2014-11-07 03:48:03 +03:00
import (
2014-11-13 02:41:11 +03:00
"net/http"
2014-11-07 03:48:03 +03:00
"testing"
2014-11-13 02:41:11 +03:00
"time"
2014-11-07 03:48:03 +03:00
"github.com/coreos/etcd/etcdserver/stats"
2014-11-13 02:41:11 +03:00
"github.com/coreos/etcd/pkg/testutil"
2014-11-07 03:48:03 +03:00
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/raft/raftpb"
2014-11-07 03:48:03 +03:00
)
2014-12-29 04:44:26 +03:00
func TestTransportAdd(t *testing.T) {
2014-11-07 03:48:03 +03:00
ls := stats.NewLeaderStats("")
2014-12-29 23:20:52 +03:00
tr := &transport{
leaderStats: ls,
2014-12-29 23:20:52 +03:00
peers: make(map[types.ID]*peer),
2014-12-29 04:44:26 +03:00
}
tr.AddPeer(1, []string{"http://a"})
2014-11-07 03:48:03 +03:00
if _, ok := ls.Followers["1"]; !ok {
t.Errorf("FollowerStats[1] is nil, want exists")
}
2014-12-29 04:44:26 +03:00
s, ok := tr.peers[types.ID(1)]
2014-11-07 03:48:03 +03:00
if !ok {
t.Fatalf("senders[1] is nil, want exists")
}
2014-12-29 04:44:26 +03:00
// duplicate AddPeer is ignored
tr.AddPeer(1, []string{"http://a"})
ns := tr.peers[types.ID(1)]
if s != ns {
t.Errorf("sender = %v, want %v", ns, s)
}
2014-11-07 03:48:03 +03:00
}
2014-12-29 04:44:26 +03:00
func TestTransportRemove(t *testing.T) {
2014-12-29 23:20:52 +03:00
tr := &transport{
leaderStats: stats.NewLeaderStats(""),
2014-12-29 23:20:52 +03:00
peers: make(map[types.ID]*peer),
2014-12-29 04:44:26 +03:00
}
tr.AddPeer(1, []string{"http://a"})
tr.RemovePeer(types.ID(1))
2014-11-07 03:48:03 +03:00
2014-12-29 04:44:26 +03:00
if _, ok := tr.peers[types.ID(1)]; ok {
2014-11-07 03:48:03 +03:00
t.Fatalf("senders[1] exists, want removed")
}
}
2014-11-13 02:41:11 +03:00
2015-01-03 07:00:29 +03:00
func TestTransportErrorc(t *testing.T) {
errorc := make(chan error, 1)
2014-12-29 23:20:52 +03:00
tr := &transport{
roundTripper: newRespRoundTripper(http.StatusForbidden, nil),
leaderStats: stats.NewLeaderStats(""),
2014-12-29 23:20:52 +03:00
peers: make(map[types.ID]*peer),
2015-01-03 07:00:29 +03:00
errorc: errorc,
2014-12-29 04:44:26 +03:00
}
tr.AddPeer(1, []string{"http://a"})
2014-11-14 10:03:34 +03:00
select {
2015-01-03 07:00:29 +03:00
case <-errorc:
t.Fatalf("received unexpected from errorc")
2014-11-14 10:03:34 +03:00
case <-time.After(10 * time.Millisecond):
}
2014-12-29 04:44:26 +03:00
tr.peers[1].Send(raftpb.Message{})
2014-11-14 10:03:34 +03:00
testutil.ForceGosched()
select {
2015-01-03 07:00:29 +03:00
case <-errorc:
2014-11-14 10:03:34 +03:00
default:
2015-01-03 07:00:29 +03:00
t.Fatalf("cannot receive error from errorc")
2014-11-14 10:03:34 +03:00
}
}