github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/consensus_pbft/util/messagefan_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package util 18 19 import ( 20 "testing" 21 "time" 22 ) 23 24 func TestFanIn(t *testing.T) { 25 Channels := 500 26 Messages := 100 27 28 fh := NewMessageFan() 29 30 for i := 0; i < Channels; i++ { 31 c := make(chan *Message, Messages/2) 32 fh.AddFaninChannel(c) 33 go func() { 34 for j := 0; j < Messages; j++ { 35 c <- &Message{} 36 } 37 }() 38 } 39 40 r := fh.GetOutChannel() 41 42 count := 0 43 for { 44 select { 45 case <-r: 46 case <-time.After(time.Second): 47 t.Fatalf("Timed out waiting to read %d messages from channel, at message %d", Channels*Messages, count) 48 } 49 count++ 50 if count == Channels*Messages { 51 break 52 } 53 } 54 55 select { 56 case <-r: 57 t.Fatalf("Read more than %d messages from channel", Channels*Messages) 58 default: 59 } 60 61 } 62 63 func TestFanChannelClose(t *testing.T) { 64 fh := NewMessageFan() 65 c := make(chan *Message) 66 fh.AddFaninChannel(c) 67 close(c) 68 69 for i := 0; i < 100; i++ { 70 if len(fh.ins) == 0 { 71 return 72 } 73 time.Sleep(time.Millisecond) 74 } 75 76 t.Fatalf("Channel was not cleaned up") 77 }