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

Make lookupStreamBySubject public #1114

Merged
merged 8 commits into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions js.go
Expand Up @@ -1529,7 +1529,7 @@ func (js *js) subscribe(subj, queue string, cb MsgHandler, ch chan *Msg, isSync,

// Find the stream mapped to the subject if not bound to a stream already.
if o.stream == _EMPTY_ {
stream, err = js.lookupStreamBySubject(subj)
stream, err = js.StreamNameBySubject(subj)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2144,7 +2144,8 @@ type streamNamesResponse struct {
Streams []string `json:"streams"`
}

func (js *js) lookupStreamBySubject(subj string) (string, error) {
// StreamNameBySubject returns a stream name that matches the subject.
func (js *js) StreamNameBySubject(subj string) (string, error) {
Jarema marked this conversation as resolved.
Show resolved Hide resolved
Jarema marked this conversation as resolved.
Show resolved Hide resolved
var slr streamNamesResponse
req := &streamRequest{subj}
j, err := json.Marshal(req)
Expand Down
31 changes: 31 additions & 0 deletions js_test.go
Expand Up @@ -1230,3 +1230,34 @@ func TestJetStreamStreamInfoWithSubjectDetails(t *testing.T) {
t.Fatalf("expected 0 subjects details from StreamInfo, but got %d instead", len(result.State.Subjects))
}
}

func StreamNameBySubject(t *testing.T) {
Jarema marked this conversation as resolved.
Show resolved Hide resolved

Jarema marked this conversation as resolved.
Show resolved Hide resolved
s := RunBasicJetStreamServer()
defer shutdownJSServerAndRemoveStorage(t, s)

nc, js := jsClient(t, s)
defer nc.Close()

var err error

_, err = js.AddStream(&StreamConfig{
Name: "TEST",
Subjects: []string{"test.*"},
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

stream, err := js.StreamNameBySubject("test.*")
if err != nil {
t.Fatalf("lookup stream should succeed for %s", "test.*")
}
if stream != "TEST" {
t.Fatalf("returned stream should be 'TEST'")
}

if _, err := js.StreamNameBySubject("bad"); err == nil {
t.Fatalf("error should be nil, no stream with name 'bad'")
}
}
3 changes: 3 additions & 0 deletions jsm.go
Expand Up @@ -93,6 +93,9 @@ type JetStreamManager interface {

// AccountInfo retrieves info about the JetStream usage from an account.
AccountInfo(opts ...JSOpt) (*AccountInfo, error)

// Returns a stream matching given subject.
Jarema marked this conversation as resolved.
Show resolved Hide resolved
StreamNameBySubject(string) (string, error)
}

// StreamConfig will determine the properties for a stream.
Expand Down