go.dedis.ch/onet/v4@v4.0.0-pre1/network/dispatch_test.go (about)

     1  package network
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  type basicProcessor struct {
    11  	envChan chan Envelope
    12  }
    13  
    14  func (bp *basicProcessor) Process(env *Envelope) {
    15  	bp.envChan <- *env
    16  }
    17  
    18  type basicMessage struct {
    19  	Value int
    20  }
    21  
    22  var basicMessageType = RegisterMessage(&basicMessage{})
    23  
    24  func TestBlockingDispatcher(t *testing.T) {
    25  
    26  	dispatcher := NewBlockingDispatcher()
    27  	processor := &basicProcessor{make(chan Envelope, 1)}
    28  
    29  	err := dispatcher.Dispatch(&Envelope{
    30  		Msg:     basicMessage{10},
    31  		MsgType: basicMessageType})
    32  
    33  	if err == nil {
    34  		t.Error("Dispatcher should have returned an error")
    35  	}
    36  
    37  	dispatcher.RegisterProcessor(processor, basicMessageType)
    38  	dispatcher.Dispatch(&Envelope{
    39  		Msg:     basicMessage{10},
    40  		MsgType: basicMessageType})
    41  
    42  	select {
    43  	case m := <-processor.envChan:
    44  		msg, ok := m.Msg.(basicMessage)
    45  		assert.True(t, ok)
    46  		assert.Equal(t, msg.Value, 10)
    47  	default:
    48  		t.Error("No message received")
    49  	}
    50  
    51  	var found bool
    52  	dispatcher.RegisterProcessorFunc(basicMessageType, func(e *Envelope) error {
    53  		found = true
    54  		return nil
    55  	})
    56  	dispatcher.Dispatch(&Envelope{
    57  		Msg:     basicMessage{10},
    58  		MsgType: basicMessageType})
    59  
    60  	if !found {
    61  		t.Error("ProcessorFunc should have set to true")
    62  	}
    63  }
    64  
    65  func TestRoutineDispatcher(t *testing.T) {
    66  
    67  	dispatcher := NewRoutineDispatcher()
    68  	if dispatcher == nil {
    69  		t.Fatal("nil dispatcher")
    70  	}
    71  	processor := &basicProcessor{make(chan Envelope, 1)}
    72  
    73  	err := dispatcher.Dispatch(&Envelope{
    74  		Msg:     basicMessage{10},
    75  		MsgType: basicMessageType})
    76  
    77  	if err == nil {
    78  		t.Error("Dispatcher should have returned an error")
    79  	}
    80  
    81  	dispatcher.RegisterProcessor(processor, basicMessageType)
    82  	dispatcher.Dispatch(&Envelope{
    83  		Msg:     basicMessage{10},
    84  		MsgType: basicMessageType})
    85  
    86  	select {
    87  	case m := <-processor.envChan:
    88  		msg, ok := m.Msg.(basicMessage)
    89  		assert.True(t, ok)
    90  		assert.Equal(t, msg.Value, 10)
    91  	case <-time.After(100 * time.Millisecond):
    92  		t.Error("No message received")
    93  
    94  	}
    95  }
    96  
    97  func TestDefaultProcessor(t *testing.T) {
    98  	var okCh = make(chan bool, 1)
    99  	pr := defaultProcessor{func(e *Envelope) error {
   100  		okCh <- true
   101  		return nil
   102  	}}
   103  
   104  	pr.Process(&Envelope{})
   105  	select {
   106  	case <-okCh:
   107  	default:
   108  		t.Error("no ack received...")
   109  	}
   110  }