Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fixup some racing tests #52

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions conn_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gldap

import (
"bytes"
"context"
"net"
"testing"
Expand All @@ -17,10 +16,10 @@ func Test_newConn(t *testing.T) {
server, client := net.Pipe()
t.Cleanup(func() { server.Close(); client.Close() })

var buf bytes.Buffer
buf := testSafeBuf(t)
testLogger := hclog.New(&hclog.LoggerOptions{
Name: "test",
Output: &buf,
Output: buf,
})

tests := map[string]struct {
Expand Down
30 changes: 28 additions & 2 deletions coverage/coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@

<option value="file22">github.com/jimlambrt/gldap/testdirectory/testingt.go (100.0%)</option>

<option value="file23">github.com/jimlambrt/gldap/testing.go (98.5%)</option>
<option value="file23">github.com/jimlambrt/gldap/testing.go (96.4%)</option>

</select>
</div>
Expand Down Expand Up @@ -4168,7 +4168,8 @@
d.logger.Debug("not using TLS")
}</span>
<span class="cov8" title="1">go func() </span><span class="cov8" title="1">{
_ = d.s.Run(fmt.Sprintf("%s:%d", opts.withHost, opts.withPort), connOpts...)
err := d.s.Run(fmt.Sprintf("%s:%d", opts.withHost, opts.withPort), connOpts...)
require.NoError(err)
}</span>()

<span class="cov8" title="1">if v, ok := interface{}(t).(CleanupT); ok </span><span class="cov8" title="1">{
Expand Down Expand Up @@ -5475,6 +5476,7 @@
"net"
"os"
"strings"
"sync"
"testing"

ber "github.com/go-asn1-ber/asn1-ber"
Expand Down Expand Up @@ -5718,6 +5720,30 @@
require.NoError(t, err)
return string(dec.Bytes())
}</span>

type safeBuf struct {
buf *strings.Builder
mu *sync.Mutex
}

func testSafeBuf(t *testing.T) *safeBuf <span class="cov8" title="1">{
t.Helper()
return &amp;safeBuf{
mu: &amp;sync.Mutex{},
buf: &amp;strings.Builder{},
}
}</span>
func (w *safeBuf) Write(p []byte) (n int, err error) <span class="cov8" title="1">{
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Write(p)
}</span>

func (w *safeBuf) String() string <span class="cov0" title="0">{
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.String()
}</span>
</pre>

</div>
Expand Down
1 change: 1 addition & 0 deletions coverage/coverage.log
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
1695973415,88.6
1696020265,88.5
2 changes: 1 addition & 1 deletion coverage/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestMux_serve(t *testing.T) {
t.Parallel()
t.Run("no-matching-handler", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
var buf bytes.Buffer
buf := testSafeBuf(t)
testLogger := hclog.New(&hclog.LoggerOptions{
Name: "TestServer_Run-logger",
Level: hclog.Debug,
Output: &buf,
Output: buf,
})
s, err := NewServer(WithLogger(testLogger))
require.NoError(err)
Expand All @@ -48,11 +48,14 @@ func TestMux_serve(t *testing.T) {
})
t.Run("default-route", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
var buf bytes.Buffer
buf := &safeBuf{
mu: &sync.Mutex{},
buf: &strings.Builder{},
}
testLogger := hclog.New(&hclog.LoggerOptions{
Name: "TestServer_Run-logger",
Level: hclog.Debug,
Output: &buf,
Output: buf,
})
s, err := NewServer(WithLogger(testLogger))
require.NoError(err)
Expand Down
15 changes: 12 additions & 3 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gldap_test

import (
"crypto/tls"
"crypto/x509"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -167,10 +168,18 @@ func TestServer_shutdownCtx(t *testing.T) {
td := testdirectory.Start(fakeT, testdirectory.WithDefaults(t, &testdirectory.Defaults{AllowAnonymousBind: true}))
time.Sleep(1 * time.Second) // allow time so the test directory will start up.
go func() {
client := td.Conn()
defer client.Close()
certpool := x509.NewCertPool()
certpool.AppendCertsFromPEM([]byte(td.Cert()))
tlsConfig := &tls.Config{
RootCAs: certpool,
}
conn, err := ldap.DialURL(fmt.Sprintf("ldaps://localhost:%d", td.Port()), ldap.DialWithTLSConfig(tlsConfig))
if err != nil {
return
}
defer conn.Close()
for {
err := client.UnauthenticatedBind("alice")
err := conn.UnauthenticatedBind("alice")
if err != nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion testdirectory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ func Start(t TestingT, opt ...Option) *Directory {
d.logger.Debug("not using TLS")
}
go func() {
_ = d.s.Run(fmt.Sprintf("%s:%d", opts.withHost, opts.withPort), connOpts...)
err := d.s.Run(fmt.Sprintf("%s:%d", opts.withHost, opts.withPort), connOpts...)
require.NoError(err)
}()

if v, ok := interface{}(t).(CleanupT); ok {
Expand Down
27 changes: 26 additions & 1 deletion testdirectory/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/x509"
"fmt"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -123,7 +124,7 @@ func Test_Start(t *testing.T) {
})
t.Run("start-with-TestingT", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
buf := &strings.Builder{}
buf := testSafeBuf(t)
bufLogger := hclog.New(&hclog.LoggerOptions{
Name: "my-app",
Level: hclog.LevelFromString("DEBUG"),
Expand All @@ -144,6 +145,30 @@ func Test_Start(t *testing.T) {
})
}

type safeBuf struct {
buf *strings.Builder
mu *sync.Mutex
}

func testSafeBuf(t *testing.T) *safeBuf {
t.Helper()
return &safeBuf{
mu: &sync.Mutex{},
buf: &strings.Builder{},
}
}
func (w *safeBuf) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Write(p)
}

func (w *safeBuf) String() string {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.String()
}

func TestDirectory_SimpleBindResponse(t *testing.T) {
t.Parallel()
testLogger := hclog.New(&hclog.LoggerOptions{
Expand Down
25 changes: 25 additions & 0 deletions testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net"
"os"
"strings"
"sync"
"testing"

ber "github.com/go-asn1-ber/asn1-ber"
Expand Down Expand Up @@ -247,3 +248,27 @@ func TestEncodeString(t *testing.T, tag ber.Tag, s string, opt ...Option) string
require.NoError(t, err)
return string(dec.Bytes())
}

type safeBuf struct {
buf *strings.Builder
mu *sync.Mutex
}

func testSafeBuf(t *testing.T) *safeBuf {
t.Helper()
return &safeBuf{
mu: &sync.Mutex{},
buf: &strings.Builder{},
}
}
func (w *safeBuf) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.Write(p)
}

func (w *safeBuf) String() string {
w.mu.Lock()
defer w.mu.Unlock()
return w.buf.String()
}
Loading