github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/integration_test/itest/multiple_services.go (about)

     1  package itest
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"sync"
     7  	"testing"
     8  )
     9  
    10  type MultipleServices interface {
    11  	NamespacePair
    12  	Name() string
    13  	ServiceCount() int
    14  }
    15  
    16  type multipleServices struct {
    17  	NamespacePair
    18  	name         string
    19  	serviceCount int
    20  }
    21  
    22  func WithMultipleServices(np NamespacePair, name string, serviceCount int, f func(MultipleServices)) {
    23  	np.HarnessT().Run(fmt.Sprintf("Test_Services_%d", serviceCount), func(t *testing.T) {
    24  		ctx := WithT(np.HarnessContext(), t)
    25  		ms := &multipleServices{NamespacePair: np, name: name, serviceCount: serviceCount}
    26  		ms.PushHarness(ctx, ms.setup, ms.tearDown)
    27  		defer ms.PopHarness()
    28  		f(ms)
    29  	})
    30  }
    31  
    32  func (h *multipleServices) setup(ctx context.Context) bool {
    33  	wg := sync.WaitGroup{}
    34  	wg.Add(h.serviceCount)
    35  	for i := 0; i < h.serviceCount; i++ {
    36  		go func(i int) {
    37  			defer wg.Done()
    38  			h.ApplyEchoService(ctx, fmt.Sprintf("%s-%d", h.name, i), 80)
    39  		}(i)
    40  	}
    41  	wg.Wait()
    42  	return true
    43  }
    44  
    45  func (h *multipleServices) tearDown(ctx context.Context) {
    46  	wg := sync.WaitGroup{}
    47  	wg.Add(h.serviceCount)
    48  	for i := 0; i < h.serviceCount; i++ {
    49  		go func(i int) {
    50  			defer wg.Done()
    51  			h.DeleteSvcAndWorkload(ctx, "deploy", fmt.Sprintf("hello-%d", i))
    52  		}(i)
    53  	}
    54  	wg.Wait()
    55  }
    56  
    57  func (h *multipleServices) Name() string {
    58  	return h.name
    59  }
    60  
    61  func (h *multipleServices) ServiceCount() int {
    62  	return h.serviceCount
    63  }