gitee.com/mirrors/gauge@v1.0.6/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  // NewExecutionEvent creates a new execution event.
    37  func NewExecutionEvent(t Topic, i gauge.Item, r result.Result, stream int, executionInfo gauge_messages.ExecutionInfo) ExecutionEvent {
    38  	return ExecutionEvent{
    39  		Topic:         t,
    40  		Item:          i,
    41  		Result:        r,
    42  		Stream:        stream,
    43  		ExecutionInfo: executionInfo,
    44  	}
    45  }
    46  
    47  // Topic indicates the topic of ExecutionEvent
    48  type Topic int
    49  
    50  const (
    51  	SuiteStart Topic = iota
    52  	SpecStart
    53  	ScenarioStart
    54  	ConceptStart
    55  	StepStart
    56  	StepEnd
    57  	ConceptEnd
    58  	ScenarioEnd
    59  	SpecEnd
    60  	SuiteEnd
    61  )
    62  
    63  var subscriberRegistry map[Topic][]chan ExecutionEvent
    64  
    65  // InitRegistry is used for console reporting, execution API and rerun of specs
    66  func InitRegistry() {
    67  	subscriberRegistry = make(map[Topic][]chan ExecutionEvent, 0)
    68  	subscriberRegistry[SuiteStart] = make([]chan ExecutionEvent, 0)
    69  	subscriberRegistry[ScenarioStart] = make([]chan ExecutionEvent, 0)
    70  	subscriberRegistry[ConceptStart] = make([]chan ExecutionEvent, 0)
    71  	subscriberRegistry[StepStart] = make([]chan ExecutionEvent, 0)
    72  	subscriberRegistry[SuiteEnd] = make([]chan ExecutionEvent, 0)
    73  	subscriberRegistry[ConceptEnd] = make([]chan ExecutionEvent, 0)
    74  	subscriberRegistry[ScenarioEnd] = make([]chan ExecutionEvent, 0)
    75  	subscriberRegistry[SpecEnd] = make([]chan ExecutionEvent, 0)
    76  	subscriberRegistry[SuiteEnd] = make([]chan ExecutionEvent, 0)
    77  }
    78  
    79  // Register registers the given channel to the given list of topics. Any updates for the given topics
    80  // will be sent on this channel
    81  func Register(ch chan ExecutionEvent, topics ...Topic) {
    82  	for _, t := range topics {
    83  		subscriberRegistry[t] = append(subscriberRegistry[t], ch)
    84  	}
    85  }
    86  
    87  // Notify notifies all the subscribers of the event about its occurrence
    88  func Notify(e ExecutionEvent) {
    89  	for _, c := range subscriberRegistry[e.Topic] {
    90  		c <- e
    91  	}
    92  }