go-micro.dev/v5@v5.12.0/events/natsjs/helpers_test.go (about)

     1  package natsjs_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"path/filepath"
     8  	"testing"
     9  	"time"
    10  
    11  	nserver "github.com/nats-io/nats-server/v2/server"
    12  	"github.com/test-go/testify/require"
    13  )
    14  
    15  func getFreeLocalhostAddress() string {
    16  	l, _ := net.Listen("tcp", "127.0.0.1:0")
    17  	defer l.Close()
    18  	return l.Addr().String()
    19  }
    20  
    21  func natsServer(ctx context.Context, t *testing.T, opts *nserver.Options) {
    22  	t.Helper()
    23  
    24  	server, err := nserver.NewServer(
    25  		opts,
    26  	)
    27  	require.NoError(t, err)
    28  	if err != nil {
    29  		return
    30  	}
    31  
    32  	server.SetLoggerV2(
    33  		NewLogWrapper(),
    34  		true, true, false,
    35  	)
    36  
    37  	// first start NATS
    38  	go server.Start()
    39  	ready := server.ReadyForConnections(time.Second * 10)
    40  	if !ready {
    41  		t.Fatalf("NATS server not ready")
    42  	}
    43  	jsConf := &nserver.JetStreamConfig{
    44  		StoreDir: filepath.Join(t.TempDir(), "nats-js"),
    45  	}
    46  
    47  	// second start JetStream
    48  	err = server.EnableJetStream(jsConf)
    49  	require.NoError(t, err)
    50  	if err != nil {
    51  		return
    52  	}
    53  
    54  	<-ctx.Done()
    55  
    56  	server.Shutdown()
    57  }
    58  
    59  func NewLogWrapper() *LogWrapper {
    60  	return &LogWrapper{}
    61  }
    62  
    63  type LogWrapper struct {
    64  }
    65  
    66  // Noticef logs a notice statement.
    67  func (l *LogWrapper) Noticef(format string, v ...interface{}) {
    68  	fmt.Printf(format+"\n", v...)
    69  }
    70  
    71  // Warnf logs a warning statement.
    72  func (l *LogWrapper) Warnf(format string, v ...interface{}) {
    73  	fmt.Printf(format+"\n", v...)
    74  }
    75  
    76  // Fatalf logs a fatal statement.
    77  func (l *LogWrapper) Fatalf(format string, v ...interface{}) {
    78  	fmt.Printf(format+"\n", v...)
    79  }
    80  
    81  // Errorf logs an error statement.
    82  func (l *LogWrapper) Errorf(format string, v ...interface{}) {
    83  	fmt.Printf(format+"\n", v...)
    84  }
    85  
    86  // Debugf logs a debug statement.
    87  func (l *LogWrapper) Debugf(format string, v ...interface{}) {
    88  	fmt.Printf(format+"\n", v...)
    89  }
    90  
    91  // Tracef logs a trace statement.
    92  func (l *LogWrapper) Tracef(format string, v ...interface{}) {
    93  	fmt.Printf(format+"\n", v...)
    94  }