github.com/getgauge/gauge@v1.6.9/execution/event/event.go (about)

     1  /*----------------------------------------------------------------
     2   *  Copyright (c) ThoughtWorks, Inc.
     3   *  Licensed under the Apache License, Version 2.0
     4   *  See LICENSE in the project root for license information.
     5   *----------------------------------------------------------------*/
     6  
     7  package event
     8  
     9  import (
    10  	"github.com/getgauge/gauge-proto/go/gauge_messages"
    11  	"github.com/getgauge/gauge/execution/result"
    12  	"github.com/getgauge/gauge/gauge"
    13  )
    14  
    15  // ExecutionEvent represents an event raised during various phases of the
    16  // execution lifecycle. This is only for execution and excludes parsing, validation, etc.
    17  type ExecutionEvent struct {
    18  	Topic         Topic
    19  	Item          gauge.Item
    20  	Result        result.Result
    21  	Stream        int
    22  	ExecutionInfo *gauge_messages.ExecutionInfo
    23  }
    24  
    25  // NewExecutionEvent creates a new execution event.
    26  func NewExecutionEvent(t Topic, i gauge.Item, r result.Result, stream int, executionInfo *gauge_messages.ExecutionInfo) ExecutionEvent {
    27  	return ExecutionEvent{
    28  		Topic:         t,
    29  		Item:          i,
    30  		Result:        r,
    31  		Stream:        stream,
    32  		ExecutionInfo: executionInfo,
    33  	}
    34  }
    35  
    36  // Topic indicates the topic of ExecutionEvent
    37  type Topic int
    38  
    39  const (
    40  	SuiteStart Topic = iota
    41  	SpecStart
    42  	ScenarioStart
    43  	ConceptStart
    44  	StepStart
    45  	StepEnd
    46  	ConceptEnd
    47  	ScenarioEnd
    48  	SpecEnd
    49  	SuiteEnd
    50  )
    51  
    52  var subscriberRegistry map[Topic][]chan ExecutionEvent
    53  
    54  // InitRegistry is used for console reporting, execution API and rerun of specs
    55  func InitRegistry() {
    56  	subscriberRegistry = make(map[Topic][]chan ExecutionEvent)
    57  	subscriberRegistry[SuiteStart] = make([]chan ExecutionEvent, 0)
    58  	subscriberRegistry[ScenarioStart] = make([]chan ExecutionEvent, 0)
    59  	subscriberRegistry[ConceptStart] = make([]chan ExecutionEvent, 0)
    60  	subscriberRegistry[StepStart] = make([]chan ExecutionEvent, 0)
    61  	subscriberRegistry[SuiteEnd] = make([]chan ExecutionEvent, 0)
    62  	subscriberRegistry[ConceptEnd] = make([]chan ExecutionEvent, 0)
    63  	subscriberRegistry[ScenarioEnd] = make([]chan ExecutionEvent, 0)
    64  	subscriberRegistry[SpecEnd] = make([]chan ExecutionEvent, 0)
    65  	subscriberRegistry[SuiteEnd] = make([]chan ExecutionEvent, 0)
    66  }
    67  
    68  // Register registers the given channel to the given list of topics. Any updates for the given topics
    69  // will be sent on this channel
    70  func Register(ch chan ExecutionEvent, topics ...Topic) {
    71  	for _, t := range topics {
    72  		subscriberRegistry[t] = append(subscriberRegistry[t], ch)
    73  	}
    74  }
    75  
    76  // Notify notifies all the subscribers of the event about its occurrence
    77  func Notify(e ExecutionEvent) {
    78  	for _, c := range subscriberRegistry[e.Topic] {
    79  		c <- e
    80  	}
    81  }