github.com/annchain/OG@v0.0.9/wserver/conn_test.go (about)

     1  // Copyright © 2019 Annchain Authors <EMAIL ADDRESS>
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package wserver
    15  
    16  import (
    17  	"fmt"
    18  	"github.com/gorilla/websocket"
    19  	"testing"
    20  )
    21  
    22  func TestEvent2Conns(t *testing.T) {
    23  	e2c := NewEvent2Cons()
    24  	for i := 0; i < 10; i++ {
    25  		con := NewConn(&websocket.Conn{})
    26  		e2c.Add(EVENT_NEW_UNIT, con)
    27  	}
    28  	conns, err := e2c.Get(EVENT_NEW_UNIT)
    29  	firstLen := len(conns)
    30  	if err != nil {
    31  		t.Errorf("Get EVENT_NEW_UNIT error: %s\n", err)
    32  	} else {
    33  		show(conns)
    34  	}
    35  
    36  	e2c.Remove(EVENT_NEW_UNIT, conns[0])
    37  	conns, _ = e2c.Get(EVENT_NEW_UNIT)
    38  	secondLen := len(conns)
    39  	if firstLen == secondLen {
    40  		t.Errorf("Remove error\n")
    41  	} else {
    42  		fmt.Printf("Remove successfully\n")
    43  	}
    44  	show(conns)
    45  
    46  	c, err := e2c.GetWithID(EVENT_NEW_UNIT, conns[0].GetID())
    47  	if err != nil {
    48  		t.Errorf("%s\n", err)
    49  	} else {
    50  		if c.GetID() == conns[0].GetID() {
    51  			fmt.Println("Got conn with ID OK")
    52  		}
    53  	}
    54  
    55  }
    56  
    57  func show(conns []*Conn) {
    58  	for i, c := range conns {
    59  		fmt.Printf("%d-th conn ID: %s\n", i, c.GetID())
    60  	}
    61  }