-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added embedded nats server (#5564)
* feat: added embedded nats server * chore: added comments to the code
- Loading branch information
Showing
4 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package event | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/nats-io/nats-server/v2/server" | ||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
const natsConnectionTimeout = 10 * time.Second | ||
|
||
var ErrNatsEmbeddedServerTimeout = fmt.Errorf("server not ready for connections in " + natsConnectionTimeout.String()) | ||
|
||
// ServerWithConnection starts NATS server with embedded JetStream, wait for readines and returns connection to it | ||
func ServerWithConnection(dir string) (*server.Server, *nats.Conn, error) { | ||
opts := &server.Options{ | ||
JetStream: true, | ||
Port: 4222, | ||
Host: "localhost", | ||
StoreDir: dir, | ||
NoLog: false, | ||
NoSigs: true, | ||
MaxControlLine: 4096, | ||
DisableShortFirstPing: true, | ||
} | ||
|
||
// Initialize new server with options | ||
ns, err := server.NewServer(opts) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// Start the server via goroutine | ||
ns.Start() | ||
ns.EnableJetStream(&server.JetStreamConfig{ | ||
StoreDir: dir, | ||
}) | ||
|
||
// Wait for server to be ready for connections - this one will block | ||
if !ns.ReadyForConnections(natsConnectionTimeout) { | ||
return nil, nil, ErrNatsEmbeddedServerTimeout | ||
} | ||
|
||
// Connect to server | ||
nc, err := nats.Connect(ns.ClientURL()) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
return ns, nc, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package event | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEmbeddedServer_Start(t *testing.T) { | ||
|
||
ns, nc, err := ServerWithConnection(t.TempDir()) | ||
assert.NoError(t, err) | ||
|
||
nc.Subscribe("events", func(msg *nats.Msg) { | ||
t.Logf("Received message: %s", string(msg.Data)) | ||
ns.Shutdown() | ||
}) | ||
|
||
nc.Publish("events", []byte("test")) | ||
|
||
t.Log("Waiting for shutdown") | ||
ns.WaitForShutdown() | ||
|
||
} |