github.com/turingchain2020/turingchain@v1.1.21/system/p2p/dht/protocol/peer/pubsub_test.go (about)

     1  package peer
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	l "github.com/turingchain2020/turingchain/common/log"
     8  	"github.com/turingchain2020/turingchain/queue"
     9  	"github.com/turingchain2020/turingchain/types"
    10  	pubsub "github.com/libp2p/go-libp2p-pubsub"
    11  	pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func init() {
    16  	l.SetLogLevel("err")
    17  }
    18  
    19  //测试订阅topic
    20  func testHandleSubTopicEvent(p *Protocol, msg *queue.Message) {
    21  	p.handleEventSubTopic(msg)
    22  }
    23  
    24  //测试删除topic
    25  func testHandleRemoveTopicEvent(p *Protocol, msg *queue.Message) {
    26  	p.handleEventRemoveTopic(msg)
    27  }
    28  
    29  //测试获取topicList
    30  func testHandleGetTopicsEvent(p *Protocol, msg *queue.Message) {
    31  	p.handleEventGetTopics(msg)
    32  }
    33  
    34  //测试pubmsg
    35  func testHandlerPubMsg(p *Protocol, msg *queue.Message) {
    36  	p.handleEventPubMsg(msg)
    37  }
    38  
    39  func testSubTopic(t *testing.T, p *Protocol) {
    40  
    41  	msgs := make([]*queue.Message, 0)
    42  	msgs = append(msgs, p.QueueClient.NewMessage("p2p", types.EventSubTopic, &types.SubTopic{
    43  		Module: "blockchain",
    44  		Topic:  "bzTest",
    45  	}))
    46  
    47  	msgs = append(msgs, p.QueueClient.NewMessage("p2p", types.EventSubTopic, &types.SubTopic{
    48  		Module: "blockchain",
    49  		Topic:  "bzTest",
    50  	}))
    51  
    52  	msgs = append(msgs, p.QueueClient.NewMessage("p2p", types.EventSubTopic, &types.SubTopic{
    53  		Module: "mempool",
    54  		Topic:  "bzTest",
    55  	}))
    56  
    57  	msgs = append(msgs, p.QueueClient.NewMessage("p2p", types.EventSubTopic, &types.SubTopic{
    58  		Module: "rpc",
    59  		Topic:  "rtopic",
    60  	}))
    61  
    62  	msgs = append(msgs, p.QueueClient.NewMessage("p2p", types.EventSubTopic, &types.SubTopic{
    63  		Module: "rpc",
    64  		Topic:  "rtopic",
    65  	}))
    66  
    67  	//发送订阅请求
    68  	for _, msg := range msgs {
    69  		testHandleSubTopicEvent(p, msg)
    70  		replyMsg, err := p.QueueClient.WaitTimeout(msg, time.Second*10)
    71  		require.Nil(t, err)
    72  
    73  		subReply, ok := replyMsg.GetData().(*types.Reply)
    74  		if ok {
    75  			t.Log("subReply status", subReply.IsOk)
    76  			if subReply.IsOk {
    77  				var reply types.SubTopicReply
    78  				types.Decode(subReply.GetMsg(), &reply)
    79  				require.NotNil(t, reply)
    80  				t.Log("reply", reply.GetMsg())
    81  			} else {
    82  				//订阅失败
    83  				t.Log("subfailed Reply ", string(subReply.GetMsg()))
    84  			}
    85  		}
    86  	}
    87  }
    88  
    89  func testPushMsg(t *testing.T, protocol *Protocol) {
    90  	pubTopicMsg := protocol.QueueClient.NewMessage("p2p", types.EventPubTopicMsg, &types.PublishTopicMsg{Topic: "bzTest", Msg: []byte("one two tree four")})
    91  	testHandlerPubMsg(protocol, pubTopicMsg)
    92  	resp, err := protocol.QueueClient.WaitTimeout(pubTopicMsg, time.Second*10)
    93  	require.Nil(t, err)
    94  	rpy := resp.GetData().(*types.Reply)
    95  	t.Log("bzTest isok", rpy.IsOk, "msg", string(rpy.GetMsg()))
    96  	require.True(t, rpy.IsOk)
    97  	//换个不存在的topic测试
    98  	pubTopicMsg = protocol.QueueClient.NewMessage("p2p", types.EventPubTopicMsg, &types.PublishTopicMsg{Topic: "bzTest2", Msg: []byte("one two tree four")})
    99  	testHandlerPubMsg(protocol, pubTopicMsg)
   100  	resp, err = protocol.QueueClient.WaitTimeout(pubTopicMsg, time.Second*10)
   101  	require.Nil(t, err)
   102  	rpy = resp.GetData().(*types.Reply)
   103  	t.Log("bzTest2 isok", rpy.IsOk, "msg", string(rpy.GetMsg()))
   104  	require.False(t, rpy.IsOk)
   105  	errPubTopicMsg := protocol.QueueClient.NewMessage("p2p", types.EventPubTopicMsg, &types.FetchTopicList{})
   106  	testHandlerPubMsg(protocol, errPubTopicMsg)
   107  
   108  }
   109  
   110  //测试获取topicList
   111  func testFetchTopics(t *testing.T, protocol *Protocol) []string {
   112  	//获取topicList
   113  	fetchTopicMsg := protocol.QueueClient.NewMessage("p2p", types.EventFetchTopics, &types.FetchTopicList{})
   114  	testHandleGetTopicsEvent(protocol, fetchTopicMsg)
   115  	resp, err := protocol.QueueClient.WaitTimeout(fetchTopicMsg, time.Second*10)
   116  	require.Nil(t, err)
   117  	//获取topicList
   118  	require.True(t, resp.GetData().(*types.Reply).GetIsOk())
   119  	var topiclist types.TopicList
   120  	err = types.Decode(resp.GetData().(*types.Reply).GetMsg(), &topiclist)
   121  	require.Nil(t, err)
   122  
   123  	t.Log("topicList", topiclist.GetTopics())
   124  
   125  	errFetchTopicMsg := protocol.QueueClient.NewMessage("p2p", types.EventFetchTopics, &types.PublishTopicMsg{})
   126  	testHandleGetTopicsEvent(protocol, errFetchTopicMsg)
   127  	return topiclist.GetTopics()
   128  }
   129  
   130  //测试推送订阅的消息内容
   131  func testSendTopicData(t *testing.T, protocol *Protocol) {
   132  	//发送收到的订阅消息,预期mempool,blockchain模块都会收到 hello,world 1
   133  	//protocol.msgChan <- &types.TopicData{Topic: "bzTest", From: "123435555", Data: []byte("hello,world 1")}
   134  	msg := &pubsub.Message{Message: &pubsub_pb.Message{Data: []byte("hello,world 1")}, ReceivedFrom: "123435555"}
   135  	protocol.subCallBack("bzTest", msg)
   136  
   137  }
   138  
   139  //测试删除某一个module的topic
   140  func testRemoveModuleTopic(t *testing.T, p *Protocol, topic, module string) {
   141  	//删除topic
   142  	removetopic := p.QueueClient.NewMessage("p2p", types.EventRemoveTopic, &types.RemoveTopic{
   143  		Topic:  topic,
   144  		Module: module,
   145  	})
   146  	//预期只有mempool模块收到hello,world 2
   147  	testHandleRemoveTopicEvent(p, removetopic) //删除blockchain的订阅消息
   148  	msg := &pubsub.Message{Message: &pubsub_pb.Message{Data: []byte("hello,world 2")}, ReceivedFrom: "123435555"}
   149  	p.subCallBack("bzTest", msg)
   150  	//p.msgChan <- &types.TopicData{Topic: "bzTest", From: "123435555", Data: []byte("hello,world 2")}
   151  
   152  	errRemovetopic := p.QueueClient.NewMessage("p2p", types.EventRemoveTopic, &types.FetchTopicList{})
   153  	testHandleRemoveTopicEvent(p, errRemovetopic) //删除blockchain的订阅消息
   154  
   155  	errRemovetopic = p.QueueClient.NewMessage("p2p", types.EventRemoveTopic, &types.RemoveTopic{Topic: "haha",
   156  		Module: module})
   157  	testHandleRemoveTopicEvent(p, errRemovetopic) //删除blockchain的订阅消息
   158  }
   159  
   160  func testBlockRecvSubData(t *testing.T, q queue.Queue) {
   161  	client := q.Client()
   162  	client.Sub(blockchain)
   163  	go func() {
   164  		for msg := range client.Recv() {
   165  			switch msg.Ty {
   166  			case types.EventReceiveSubData:
   167  				if req, ok := msg.GetData().(*types.TopicData); ok {
   168  					t.Log("blockchain Recv from", req.GetFrom(), "topic:", req.GetTopic(), "data", string(req.GetData()))
   169  				} else {
   170  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
   171  				}
   172  			}
   173  		}
   174  	}()
   175  }
   176  
   177  func testMempoolRecvSubData(t *testing.T, q queue.Queue) {
   178  	client := q.Client()
   179  	client.Sub(mempool)
   180  	go func() {
   181  		for msg := range client.Recv() {
   182  			switch msg.Ty {
   183  			case types.EventReceiveSubData:
   184  				if req, ok := msg.GetData().(*types.TopicData); ok {
   185  					t.Log("mempool Recv", req.GetFrom(), "topic:", req.GetTopic(), "data", string(req.GetData()))
   186  				} else {
   187  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
   188  				}
   189  			}
   190  		}
   191  	}()
   192  }
   193  
   194  func TestPubSub(t *testing.T) {
   195  	q := queue.New("test")
   196  	testBlockRecvSubData(t, q)
   197  	testMempoolRecvSubData(t, q)
   198  	protocol := initEnv(t, q)
   199  	testSubTopic(t, protocol) //订阅topic
   200  
   201  	topics := testFetchTopics(t, protocol) //获取topic list
   202  	require.Equal(t, len(topics), 2)
   203  	testSendTopicData(t, protocol) //通过chan推送接收到的消息
   204  
   205  	testPushMsg(t, protocol)                                   //发布消息
   206  	testRemoveModuleTopic(t, protocol, "bzTest", "blockchain") //删除某一个模块的topic
   207  	topics = testFetchTopics(t, protocol)                      //获取topic list
   208  	require.Equal(t, len(topics), 2)
   209  	//--------
   210  	testRemoveModuleTopic(t, protocol, "rtopic", "rpc") //删除某一个模块的topic
   211  	topics = testFetchTopics(t, protocol)
   212  	//t.Log("after Remove rtopic", topics)
   213  	require.Equal(t, 1, len(topics))
   214  	testRemoveModuleTopic(t, protocol, "bzTest", "mempool") //删除某一个模块的topic
   215  	topics = testFetchTopics(t, protocol)
   216  	//t.Log("after Remove bzTest", topics)
   217  	require.Equal(t, 0, len(topics))
   218  
   219  	time.Sleep(time.Second)
   220  }