<commit_before>package supervisor_test

import (
	"context"
	"fmt"
	"sync"

	"cirello.io/supervisor"
)

type Simpleservice struct {
	id int
	sync.WaitGroup
}

func (s *Simpleservice) Serve(ctx context.Context) {
	fmt.Println(s.String())
	s.Done()
	<-ctx.Done()
}

func (s *Simpleservice) String() string {
	return fmt.Sprintf("simple service %d", s.id)
}

func ExampleAddFunc() {
	var wg sync.WaitGroup

	wg.Add(1)
	supervisor.AddFunc(func(ctx context.Context) {
		fmt.Println("anonymous service")
		wg.Done()
		<-ctx.Done()
	})

	ctx, cancel := context.WithCancel(context.Background())
	go supervisor.ServeContext(ctx)

	wg.Wait()
	cancel()

	// output:
	// anonymous service<commit_msg>Fixing race condition between tests<commit_after>package supervisor_test

import (
	"context"
	"fmt"
	"sync"

	"cirello.io/supervisor"
)

type Simpleservice struct {
	id int
	sync.WaitGroup
}

func (s *Simpleservice) Serve(ctx context.Context) {
	fmt.Println(s.String())
	s.Done()
	<-ctx.Done()
}

func (s *Simpleservice) String() string {
	return fmt.Sprintf("simple service %d", s.id)
}

func ExampleAddFunc() {
	var svc sync.WaitGroup

	svc.Add(1)
	supervisor.AddFunc(func(ctx context.Context) {
		fmt.Println("anonymous service")
		svc.Done()
		<-ctx.Done()
	})

	ctx, cancel := context.WithCancel(context.Background())
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
		supervisor.ServeContext(ctx)
		wg.Done()
	}()

	svc.Wait()
	cancel()
	wg.Wait()

	// output:
	// anonymous service
{'fraction_non_alphanumeric': 0.15322033898305085, 'fraction_numerical': 0.002033898305084746, 'mean_word_length': 4.197183098591549, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2242843', 'n_tokens_mistral': 593, 'n_tokens_neox': 557, 'n_words': 116}