github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/utilities/event/example_feed_test.go (about)

     1  package event_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/neatio-net/neatio/utilities/event"
     7  )
     8  
     9  func ExampleFeed_acknowledgedEvents() {
    10  
    11  	var feed event.Feed
    12  	type ackedEvent struct {
    13  		i   int
    14  		ack chan<- struct{}
    15  	}
    16  
    17  	done := make(chan struct{})
    18  	defer close(done)
    19  	for i := 0; i < 3; i++ {
    20  		ch := make(chan ackedEvent, 100)
    21  		sub := feed.Subscribe(ch)
    22  		go func() {
    23  			defer sub.Unsubscribe()
    24  			for {
    25  				select {
    26  				case ev := <-ch:
    27  					fmt.Println(ev.i)
    28  					ev.ack <- struct{}{}
    29  				case <-done:
    30  					return
    31  				}
    32  			}
    33  		}()
    34  	}
    35  
    36  	for i := 0; i < 3; i++ {
    37  		acksignal := make(chan struct{})
    38  		n := feed.Send(ackedEvent{i, acksignal})
    39  		for ack := 0; ack < n; ack++ {
    40  			<-acksignal
    41  		}
    42  	}
    43  
    44  }