How to use ForAll method of wait Package

Best Testcontainers-go code snippet using wait.ForAll

openldap_test.go

Source:openldap_test.go Github

copy

Full Screen

...56 Env: map[string]string{57 "LDAP_ADMIN_USERNAME": "manager",58 "LDAP_ADMIN_PASSWORD": "secret",59 },60 WaitingFor: wait.ForAll(61 wait.ForLog("slapd starting"),62 wait.ForListeningPort(nat.Port(servicePort)),63 ),64 }65 err := container.Start()66 require.NoError(t, err, "failed to start container")67 defer func() {68 require.NoError(t, container.Terminate(), "terminating container failed")69 }()70 port, err := strconv.Atoi(container.Ports[servicePort])71 require.NoError(t, err)72 o := &Openldap{73 Host: container.Address,74 Port: port,75 BindDn: "CN=manager,DC=example,DC=org",76 BindPassword: "secret",77 }78 var acc testutil.Accumulator79 err = o.Gather(&acc)80 require.NoError(t, err)81 commonTests(t, o, &acc)82}83func TestOpenldapStartTLSIntegration(t *testing.T) {84 if testing.Short() {85 t.Skip("Skipping integration test in short mode")86 }87 pki := testutil.NewPKI("../../../testutil/pki")88 tlsPem, err := filepath.Abs(pki.ServerCertAndKeyPath())89 require.NoError(t, err)90 tlsCert, err := filepath.Abs(pki.ServerCertPath())91 require.NoError(t, err)92 tlsKey, err := filepath.Abs(pki.ServerKeyPath())93 require.NoError(t, err)94 container := testutil.Container{95 Image: "bitnami/openldap",96 ExposedPorts: []string{servicePort},97 Env: map[string]string{98 "LDAP_ADMIN_USERNAME": "manager",99 "LDAP_ADMIN_PASSWORD": "secret",100 "LDAP_ENABLE_TLS": "yes",101 "LDAP_TLS_CA_FILE": "server.pem",102 "LDAP_TLS_CERT_FILE": "server.crt",103 "LDAP_TLS_KEY_FILE": "server.key",104 },105 BindMounts: map[string]string{106 "/server.pem": tlsPem,107 "/server.crt": tlsCert,108 "/server.key": tlsKey,109 },110 WaitingFor: wait.ForAll(111 wait.ForLog("slapd starting"),112 wait.ForListeningPort(nat.Port(servicePort)),113 ),114 }115 err = container.Start()116 require.NoError(t, err, "failed to start container")117 defer func() {118 require.NoError(t, container.Terminate(), "terminating container failed")119 }()120 port, err := strconv.Atoi(container.Ports[servicePort])121 require.NoError(t, err)122 cert, err := filepath.Abs(pki.ClientCertPath())123 require.NoError(t, err)124 o := &Openldap{125 Host: container.Address,126 Port: port,127 SSL: "starttls",128 InsecureSkipVerify: true,129 BindDn: "CN=manager,DC=example,DC=org",130 BindPassword: "secret",131 TLSCA: cert,132 }133 var acc testutil.Accumulator134 err = o.Gather(&acc)135 require.NoError(t, err)136 commonTests(t, o, &acc)137}138func TestOpenldapLDAPSIntegration(t *testing.T) {139 if testing.Short() {140 t.Skip("Skipping integration test in short mode")141 }142 pki := testutil.NewPKI("../../../testutil/pki")143 tlsPem, err := filepath.Abs(pki.ServerCertAndKeyPath())144 require.NoError(t, err)145 tlsCert, err := filepath.Abs(pki.ServerCertPath())146 require.NoError(t, err)147 tlsKey, err := filepath.Abs(pki.ServerKeyPath())148 require.NoError(t, err)149 container := testutil.Container{150 Image: "bitnami/openldap",151 ExposedPorts: []string{servicePortSecure},152 Env: map[string]string{153 "LDAP_ADMIN_USERNAME": "manager",154 "LDAP_ADMIN_PASSWORD": "secret",155 "LDAP_ENABLE_TLS": "yes",156 "LDAP_TLS_CA_FILE": "server.pem",157 "LDAP_TLS_CERT_FILE": "server.crt",158 "LDAP_TLS_KEY_FILE": "server.key",159 },160 BindMounts: map[string]string{161 "/server.pem": tlsPem,162 "/server.crt": tlsCert,163 "/server.key": tlsKey,164 },165 WaitingFor: wait.ForAll(166 wait.ForLog("slapd starting"),167 wait.ForListeningPort(nat.Port(servicePortSecure)),168 ),169 }170 err = container.Start()171 require.NoError(t, err, "failed to start container")172 defer func() {173 require.NoError(t, container.Terminate(), "terminating container failed")174 }()175 port, err := strconv.Atoi(container.Ports[servicePortSecure])176 require.NoError(t, err)177 o := &Openldap{178 Host: container.Address,179 Port: port,180 SSL: "ldaps",181 InsecureSkipVerify: true,182 BindDn: "CN=manager,DC=example,DC=org",183 BindPassword: "secret",184 }185 var acc testutil.Accumulator186 err = o.Gather(&acc)187 require.NoError(t, err)188 commonTests(t, o, &acc)189}190func TestOpenldapInvalidSSLIntegration(t *testing.T) {191 if testing.Short() {192 t.Skip("Skipping integration test in short mode")193 }194 pki := testutil.NewPKI("../../../testutil/pki")195 tlsPem, err := filepath.Abs(pki.ServerCertAndKeyPath())196 require.NoError(t, err)197 tlsCert, err := filepath.Abs(pki.ServerCertPath())198 require.NoError(t, err)199 tlsKey, err := filepath.Abs(pki.ServerKeyPath())200 require.NoError(t, err)201 container := testutil.Container{202 Image: "bitnami/openldap",203 ExposedPorts: []string{servicePortSecure},204 Env: map[string]string{205 "LDAP_ADMIN_USERNAME": "manager",206 "LDAP_ADMIN_PASSWORD": "secret",207 "LDAP_ENABLE_TLS": "yes",208 "LDAP_TLS_CA_FILE": "server.pem",209 "LDAP_TLS_CERT_FILE": "server.crt",210 "LDAP_TLS_KEY_FILE": "server.key",211 },212 BindMounts: map[string]string{213 "/server.pem": tlsPem,214 "/server.crt": tlsCert,215 "/server.key": tlsKey,216 },217 WaitingFor: wait.ForAll(218 wait.ForLog("slapd starting"),219 wait.ForListeningPort(nat.Port(servicePortSecure)),220 ),221 }222 err = container.Start()223 require.NoError(t, err, "failed to start container")224 defer func() {225 require.NoError(t, container.Terminate(), "terminating container failed")226 }()227 port, err := strconv.Atoi(container.Ports[servicePortSecure])228 require.NoError(t, err)229 o := &Openldap{230 Host: container.Address,231 Port: port,232 SSL: "invalid",233 InsecureSkipVerify: true,234 }235 var acc testutil.Accumulator236 err = o.Gather(&acc)237 require.NoError(t, err) // test that we didn't return an error238 require.Zero(t, acc.NFields()) // test that we didn't return any fields239 require.NotEmpty(t, acc.Errors) // test that we set an error240}241func TestOpenldapBindIntegration(t *testing.T) {242 if testing.Short() {243 t.Skip("Skipping integration test in short mode")244 }245 container := testutil.Container{246 Image: "bitnami/openldap",247 ExposedPorts: []string{servicePort},248 Env: map[string]string{249 "LDAP_ADMIN_USERNAME": "manager",250 "LDAP_ADMIN_PASSWORD": "secret",251 },252 WaitingFor: wait.ForAll(253 wait.ForLog("slapd starting"),254 wait.ForListeningPort(nat.Port(servicePort)),255 ),256 }257 err := container.Start()258 require.NoError(t, err, "failed to start container")259 defer func() {260 require.NoError(t, container.Terminate(), "terminating container failed")261 }()262 port, err := strconv.Atoi(container.Ports[servicePort])263 require.NoError(t, err)264 o := &Openldap{265 Host: container.Address,266 Port: port,267 SSL: "",268 InsecureSkipVerify: true,269 BindDn: "CN=manager,DC=example,DC=org",270 BindPassword: "secret",271 }272 var acc testutil.Accumulator273 err = o.Gather(&acc)274 require.NoError(t, err)275 commonTests(t, o, &acc)276}277func commonTests(t *testing.T, o *Openldap, acc *testutil.Accumulator) {278 // helpful local commands to run:279 // ldapwhoami -D "CN=manager,DC=example,DC=org" -H ldap://localhost:1389 -w secret280 // ldapsearch -D "CN=manager,DC=example,DC=org" -H "ldap://localhost:1389" -b cn=Monitor -w secret281 require.Empty(t, acc.Errors, "accumulator had no errors")282 require.True(t, acc.HasMeasurement("openldap"), "Has a measurement called 'openldap'")283 require.Equal(t, o.Host, acc.TagValue("openldap", "server"), "Has a tag value of server=o.Host")284 require.Equal(t, strconv.Itoa(o.Port), acc.TagValue("openldap", "port"), "Has a tag value of port=o.Port")285 require.True(t, acc.HasInt64Field("openldap", "total_connections"), "Has an integer field called total_connections")286}287func TestOpenldapReverseMetricsIntegration(t *testing.T) {288 if testing.Short() {289 t.Skip("Skipping integration test in short mode")290 }291 container := testutil.Container{292 Image: "bitnami/openldap",293 ExposedPorts: []string{servicePort},294 Env: map[string]string{295 "LDAP_ADMIN_USERNAME": "manager",296 "LDAP_ADMIN_PASSWORD": "secret",297 },298 WaitingFor: wait.ForAll(299 wait.ForLog("slapd starting"),300 wait.ForListeningPort(nat.Port(servicePort)),301 ),302 }303 err := container.Start()304 require.NoError(t, err, "failed to start container")305 defer func() {306 require.NoError(t, container.Terminate(), "terminating container failed")307 }()308 port, err := strconv.Atoi(container.Ports[servicePort])309 require.NoError(t, err)310 o := &Openldap{311 Host: container.Address,312 Port: port,...

Full Screen

Full Screen

co.go

Source:co.go Github

copy

Full Screen

...17 }(f)18 }19 wg.Wait()20}21// ParForAll uses an iterator to execute an iterand function on each value22// returned by the iterator, using a background goroutine for each iteration.23// An iterator can be an array, a slice, a map, or an int. For arrays, and24// slices, the iterand function must accept an int index as the only argument.25// For maps, the iterand function must accept a key as the only argument. For26// ints, the iterand function must accept an int, in the range [0, n), as the27// only argument. This function blocks until all goroutines have terminated.28func ParForAll(iter interface{}, f interface{}) {29 funTy := reflect.TypeOf(f)30 if funTy.Kind() != reflect.Func {31 panic(fmt.Sprintf("parforall error: expected iterator got %T", iter))32 }33 fun := reflect.ValueOf(f)34 switch reflect.TypeOf(iter).Kind() {35 case reflect.Array, reflect.Slice:36 it := reflect.ValueOf(iter)37 numGoroutines := it.Len()38 var wg sync.WaitGroup39 wg.Add(numGoroutines)40 for i := 0; i < numGoroutines; i++ {41 go func(i int) {42 defer wg.Done()43 fun.Call([]reflect.Value{reflect.ValueOf(i)})44 }(i)45 }46 wg.Wait()47 case reflect.Map:48 it := reflect.ValueOf(iter)49 keys := it.MapKeys()50 numGoroutines := len(keys)51 var wg sync.WaitGroup52 wg.Add(numGoroutines)53 for i := 0; i < numGoroutines; i++ {54 go func(i int) {55 defer wg.Done()56 fun.Call([]reflect.Value{keys[i]})57 }(i)58 }59 wg.Wait()60 case reflect.Int:61 numGoroutines := int(reflect.ValueOf(iter).Int())62 var wg sync.WaitGroup63 wg.Add(numGoroutines)64 for i := 0; i < numGoroutines; i++ {65 go func(i int) {66 defer wg.Done()67 fun.Call([]reflect.Value{reflect.ValueOf(i)})68 }(i)69 }70 wg.Wait()71 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:72 panic(fmt.Sprintf("parforall error: expected int got %T", iter))73 default:74 panic(fmt.Sprintf("parforall error: expected iterator got %T", iter))75 }76}77// ForAll uses an iterator to execute an iterand function on each value78// returned by the iterator, using a background goroutine for CPU and79// distributing the iterands evenly across each goroutine. An iterator can be80// an array, a slice, a map, or an int. For arrays, and slices, the iterand81// function must accept an int index as the only argument. For maps, the82// iterand function must accept a key as the only argument. For ints, the83// iterand function must accept an int, in the range [0, n), as the only84// argument. This function blocks until all goroutines have terminated.85func ForAll(iter interface{}, f interface{}) {86 funTy := reflect.TypeOf(f)87 if funTy.Kind() != reflect.Func {88 panic(fmt.Sprintf("forall error: expected iterator got %T", iter))89 }90 fun := reflect.ValueOf(f)91 switch reflect.TypeOf(iter).Kind() {92 case reflect.Array, reflect.Slice:93 it := reflect.ValueOf(iter)94 num := it.Len()95 numGoroutines := runtime.NumCPU()96 numPerGoroutine := num/numGoroutines + 197 var wg sync.WaitGroup98 for i := 0; i < num; i += numPerGoroutine {99 wg.Add(1)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful