github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/execution/event/event.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  	"github.com/getgauge/gauge/execution/result"
    22  	"github.com/getgauge/gauge/gauge"
    23  	"github.com/getgauge/gauge/gauge_messages"
    24  )
    25  
    26  // ExecutionEvent represents an event raised during various phases of the
    27  // execution lifecycle. This is only for execution and excludes parsing, validation, etc.
    28  type ExecutionEvent struct {
    29  	Topic         Topic
    30  	Item          gauge.Item
    31  	Result        result.Result
    32  	Stream        int
    33  	ExecutionInfo gauge_messages.ExecutionInfo
    34  }
    35  
    36  func NewExecutionEvent(t Topic, i gauge.Item, r result.Result, stream int, executionInfo gauge_messages.ExecutionInfo) ExecutionEvent {
    37  	return ExecutionEvent{
    38  		Topic:         t,
    39  		Item:          i,
    40  		Result:        r,
    41  		Stream:        stream,
    42  		ExecutionInfo: executionInfo,
    43  	}
    44  }
    45  
    46  // Topic indicates the topic of ExecutionEvent
    47  type Topic int
    48  
    49  const (
    50  	SuiteStart Topic = iota
    51  	SpecStart
    52  	ScenarioStart
    53  	ConceptStart
    54  	StepStart
    55  	StepEnd
    56  	ConceptEnd
    57  	ScenarioEnd
    58  	SpecEnd
    59  	SuiteEnd
    60  )
    61  
    62  var subscriberRegistry map[Topic][]chan ExecutionEvent
    63  
    64  func InitRegistry() {
    65  	subscriberRegistry = make(map[Topic][]chan ExecutionEvent, 0)
    66  	subscriberRegistry[SuiteStart] = make([]chan ExecutionEvent, 0)
    67  	subscriberRegistry[ScenarioStart] = make([]chan ExecutionEvent, 0)
    68  	subscriberRegistry[ConceptStart] = make([]chan ExecutionEvent, 0)
    69  	subscriberRegistry[StepStart] = make([]chan ExecutionEvent, 0)
    70  	subscriberRegistry[SuiteEnd] = make([]chan ExecutionEvent, 0)
    71  	subscriberRegistry[ConceptEnd] = make([]chan ExecutionEvent, 0)
    72  	subscriberRegistry[ScenarioEnd] = make([]chan ExecutionEvent, 0)
    73  	subscriberRegistry[SpecEnd] = make([]chan ExecutionEvent, 0)
    74  	subscriberRegistry[SuiteEnd] = make([]chan ExecutionEvent, 0)
    75  }
    76  
    77  // Register registers the given channel to the given list of topics. Any updates for the given topics
    78  // will be sent on this channel
    79  func Register(ch chan ExecutionEvent, topics ...Topic) {
    80  	for _, t := range topics {
    81  		subscriberRegistry[t] = append(subscriberRegistry[t], ch)
    82  	}
    83  }
    84  
    85  // Notify notifies all the subscribers of the event about its occurrence
    86  func Notify(e ExecutionEvent) {
    87  	for _, c := range subscriberRegistry[e.Topic] {
    88  		c <- e
    89  	}
    90  }