...

Source file src/go.mongodb.org/mongo-driver/internal/eventtest/eventtest.go

Documentation: go.mongodb.org/mongo-driver/internal/eventtest

     1  // Copyright (C) MongoDB, Inc. 2022-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  // Package eventtest provides test types that are used to monitor client state and actions via the
     8  // various monitor types supported by the driver.
     9  package eventtest
    10  
    11  import (
    12  	"sync"
    13  
    14  	"go.mongodb.org/mongo-driver/event"
    15  )
    16  
    17  // TestPoolMonitor exposes an *event.TestPoolMonitor and collects all events logged to that
    18  // *event.TestPoolMonitor. It is safe to use from multiple concurrent goroutines.
    19  type TestPoolMonitor struct {
    20  	*event.PoolMonitor
    21  
    22  	events []*event.PoolEvent
    23  	mu     sync.RWMutex
    24  }
    25  
    26  func NewTestPoolMonitor() *TestPoolMonitor {
    27  	tpm := &TestPoolMonitor{
    28  		events: make([]*event.PoolEvent, 0),
    29  	}
    30  	tpm.PoolMonitor = &event.PoolMonitor{
    31  		Event: func(evt *event.PoolEvent) {
    32  			tpm.mu.Lock()
    33  			defer tpm.mu.Unlock()
    34  			tpm.events = append(tpm.events, evt)
    35  		},
    36  	}
    37  	return tpm
    38  }
    39  
    40  // Events returns a copy of the events collected by the testPoolMonitor. Filters can optionally be
    41  // applied to the returned events set and are applied using AND logic (i.e. all filters must return
    42  // true to include the event in the result).
    43  func (tpm *TestPoolMonitor) Events(filters ...func(*event.PoolEvent) bool) []*event.PoolEvent {
    44  	tpm.mu.RLock()
    45  	defer tpm.mu.RUnlock()
    46  
    47  	filtered := make([]*event.PoolEvent, 0, len(tpm.events))
    48  	for _, evt := range tpm.events {
    49  		keep := true
    50  		for _, filter := range filters {
    51  			if !filter(evt) {
    52  				keep = false
    53  				break
    54  			}
    55  		}
    56  		if keep {
    57  			filtered = append(filtered, evt)
    58  		}
    59  	}
    60  
    61  	return filtered
    62  }
    63  
    64  // ClearEvents will reset the events collected by the testPoolMonitor.
    65  func (tpm *TestPoolMonitor) ClearEvents() {
    66  	tpm.mu.Lock()
    67  	defer tpm.mu.Unlock()
    68  	tpm.events = tpm.events[:0]
    69  }
    70  
    71  // IsPoolCleared returns true if there are any events of type "event.PoolCleared" in the events
    72  // recorded by the testPoolMonitor.
    73  func (tpm *TestPoolMonitor) IsPoolCleared() bool {
    74  	poolClearedEvents := tpm.Events(func(evt *event.PoolEvent) bool {
    75  		return evt.Type == event.PoolCleared
    76  	})
    77  	return len(poolClearedEvents) > 0
    78  }
    79  
    80  // Interruptions returns the number of interruptions in the events recorded by the testPoolMonitor.
    81  func (tpm *TestPoolMonitor) Interruptions() int {
    82  	interruptions := tpm.Events(func(evt *event.PoolEvent) bool {
    83  		return evt.Interruption
    84  	})
    85  	return len(interruptions)
    86  }
    87  

View as plain text