gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/go-control-plane/pkg/test/v3/callbacks.go (about)

     1  package test
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"sync"
     7  
     8  	discovery "gitee.com/zhaochuninhefei/gmgo/go-control-plane/envoy/service/discovery/v3"
     9  )
    10  
    11  type Callbacks struct {
    12  	Signal         chan struct{}
    13  	Debug          bool
    14  	Fetches        int
    15  	Requests       int
    16  	DeltaRequests  int
    17  	DeltaResponses int
    18  	mu             sync.Mutex
    19  }
    20  
    21  func (cb *Callbacks) Report() {
    22  	cb.mu.Lock()
    23  	defer cb.mu.Unlock()
    24  	log.Printf("server callbacks fetches=%d requests=%d\n", cb.Fetches, cb.Requests)
    25  }
    26  func (cb *Callbacks) OnStreamOpen(_ context.Context, id int64, typ string) error {
    27  	if cb.Debug {
    28  		log.Printf("stream %d open for %s\n", id, typ)
    29  	}
    30  	return nil
    31  }
    32  func (cb *Callbacks) OnStreamClosed(id int64) {
    33  	if cb.Debug {
    34  		log.Printf("stream %d closed\n", id)
    35  	}
    36  }
    37  func (cb *Callbacks) OnDeltaStreamOpen(_ context.Context, id int64, typ string) error {
    38  	if cb.Debug {
    39  		log.Printf("delta stream %d open for %s\n", id, typ)
    40  	}
    41  	return nil
    42  }
    43  func (cb *Callbacks) OnDeltaStreamClosed(id int64) {
    44  	if cb.Debug {
    45  		log.Printf("delta stream %d closed\n", id)
    46  	}
    47  }
    48  func (cb *Callbacks) OnStreamRequest(int64, *discovery.DiscoveryRequest) error {
    49  	cb.mu.Lock()
    50  	defer cb.mu.Unlock()
    51  	cb.Requests++
    52  	if cb.Signal != nil {
    53  		close(cb.Signal)
    54  		cb.Signal = nil
    55  	}
    56  	return nil
    57  }
    58  func (cb *Callbacks) OnStreamResponse(context.Context, int64, *discovery.DiscoveryRequest, *discovery.DiscoveryResponse) {
    59  }
    60  
    61  //goland:noinspection GoUnusedParameter
    62  func (cb *Callbacks) OnStreamDeltaResponse(id int64, req *discovery.DeltaDiscoveryRequest, res *discovery.DeltaDiscoveryResponse) {
    63  	cb.mu.Lock()
    64  	defer cb.mu.Unlock()
    65  	cb.DeltaResponses++
    66  }
    67  
    68  //goland:noinspection GoUnusedParameter
    69  func (cb *Callbacks) OnStreamDeltaRequest(id int64, req *discovery.DeltaDiscoveryRequest) error {
    70  	cb.mu.Lock()
    71  	defer cb.mu.Unlock()
    72  	cb.DeltaRequests++
    73  	if cb.Signal != nil {
    74  		close(cb.Signal)
    75  		cb.Signal = nil
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  //goland:noinspection GoUnusedParameter
    82  func (cb *Callbacks) OnFetchRequest(_ context.Context, req *discovery.DiscoveryRequest) error {
    83  	cb.mu.Lock()
    84  	defer cb.mu.Unlock()
    85  	cb.Fetches++
    86  	if cb.Signal != nil {
    87  		close(cb.Signal)
    88  		cb.Signal = nil
    89  	}
    90  	return nil
    91  }
    92  func (cb *Callbacks) OnFetchResponse(*discovery.DiscoveryRequest, *discovery.DiscoveryResponse) {}