github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/execution/event/event_test.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package event
    19  
    20  import (
    21  	"testing"
    22  
    23  	"github.com/getgauge/gauge/execution/result"
    24  	"github.com/getgauge/gauge/gauge"
    25  	"github.com/getgauge/gauge/gauge_messages"
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  func Test(t *testing.T) { TestingT(t) }
    30  
    31  type MySuite struct{}
    32  
    33  var _ = Suite(&MySuite{})
    34  
    35  func (s *MySuite) TestInitRegistry(c *C) {
    36  	InitRegistry()
    37  
    38  	c.Assert(len(subscriberRegistry), Equals, 8)
    39  }
    40  
    41  func (s *MySuite) TestRegisterForOneTopic(c *C) {
    42  	InitRegistry()
    43  	ch := make(chan ExecutionEvent)
    44  
    45  	Register(ch, StepEnd)
    46  
    47  	c.Assert(subscriberRegistry[StepEnd][0], Equals, ch)
    48  }
    49  
    50  func (s *MySuite) TestRegisterForMultipleTopics(c *C) {
    51  	InitRegistry()
    52  	ch := make(chan ExecutionEvent)
    53  
    54  	Register(ch, StepEnd, StepStart, SpecEnd, SpecStart)
    55  
    56  	c.Assert(subscriberRegistry[StepEnd][0], Equals, ch)
    57  	c.Assert(subscriberRegistry[StepStart][0], Equals, ch)
    58  	c.Assert(subscriberRegistry[SpecEnd][0], Equals, ch)
    59  	c.Assert(subscriberRegistry[SpecEnd][0], Equals, ch)
    60  }
    61  
    62  func (s *MySuite) TestMultipleSubscribersRegisteringForMultipleEvent(c *C) {
    63  	InitRegistry()
    64  
    65  	ch1 := make(chan ExecutionEvent)
    66  	Register(ch1, StepStart, StepEnd)
    67  
    68  	ch2 := make(chan ExecutionEvent)
    69  	Register(ch2, StepStart, StepEnd)
    70  
    71  	ch3 := make(chan ExecutionEvent)
    72  	Register(ch3, SpecStart, SpecEnd, StepStart, StepEnd)
    73  
    74  	c.Assert(len(subscriberRegistry[SpecStart]), Equals, 1)
    75  	c.Assert(contains(subscriberRegistry[SpecStart], ch3), Equals, true)
    76  
    77  	c.Assert(len(subscriberRegistry[SpecEnd]), Equals, 1)
    78  	c.Assert(contains(subscriberRegistry[SpecEnd], ch3), Equals, true)
    79  
    80  	c.Assert(len(subscriberRegistry[StepStart]), Equals, 3)
    81  	c.Assert(contains(subscriberRegistry[StepStart], ch1), Equals, true)
    82  	c.Assert(contains(subscriberRegistry[StepStart], ch2), Equals, true)
    83  	c.Assert(contains(subscriberRegistry[StepStart], ch3), Equals, true)
    84  
    85  	c.Assert(len(subscriberRegistry[StepEnd]), Equals, 3)
    86  	c.Assert(contains(subscriberRegistry[StepEnd], ch1), Equals, true)
    87  	c.Assert(contains(subscriberRegistry[StepEnd], ch2), Equals, true)
    88  	c.Assert(contains(subscriberRegistry[StepEnd], ch3), Equals, true)
    89  }
    90  
    91  func (s *MySuite) TestNotify(c *C) {
    92  	InitRegistry()
    93  
    94  	ch1 := make(chan ExecutionEvent, 2)
    95  	Register(ch1, StepStart, StepEnd)
    96  
    97  	ch2 := make(chan ExecutionEvent, 2)
    98  	Register(ch2, StepStart, StepEnd)
    99  
   100  	stepText := "Hello World"
   101  	protoStep := &gauge_messages.ProtoStep{ActualText: stepText}
   102  	stepRes := result.NewStepResult(protoStep)
   103  
   104  	step := &gauge.Step{Value: stepText}
   105  	stepStartEvent := NewExecutionEvent(StepStart, step, nil, 0, gauge_messages.ExecutionInfo{})
   106  	stepEndEvent := NewExecutionEvent(StepEnd, nil, stepRes, 0, gauge_messages.ExecutionInfo{})
   107  
   108  	Notify(stepStartEvent)
   109  	c.Assert(<-ch1, DeepEquals, stepStartEvent)
   110  	c.Assert(<-ch2, DeepEquals, stepStartEvent)
   111  
   112  	Notify(stepEndEvent)
   113  	c.Assert(<-ch1, DeepEquals, stepEndEvent)
   114  	c.Assert(<-ch2, DeepEquals, stepEndEvent)
   115  }
   116  
   117  func contains(arr []chan ExecutionEvent, key chan ExecutionEvent) bool {
   118  	for _, k := range arr {
   119  		if k == key {
   120  			return true
   121  		}
   122  	}
   123  	return false
   124  }