Skip to content
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
21 changes: 12 additions & 9 deletions server/pkg/zerohub/zerohub.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,14 @@ func (z *zeroHub) CreateHubIfAbsent(hubId string, metadata string, isPermanent b
z.mu.Lock()
defer z.mu.Unlock()

if existing := z.GetHubById(hubId); existing != nil {
return nil, ErrHubAlreadyExists
}

newHub, err := z.buildHub(hubId, metadata, isPermanent)
h, created, err := z.getOrCreateHubLocked(hubId, metadata, isPermanent)
if err != nil {
return nil, err
}

z.HubStorage.Add(hubId, newHub)

return newHub, nil
if !created {
return nil, ErrHubAlreadyExists
}
return h, nil
}

// GetOrCreateHub atomically returns the existing hub with the given ID, or
Expand All @@ -116,6 +112,13 @@ func (z *zeroHub) GetOrCreateHub(hubId string, metadata string, isPermanent bool
z.mu.Lock()
defer z.mu.Unlock()

return z.getOrCreateHubLocked(hubId, metadata, isPermanent)
}

// getOrCreateHubLocked performs the check-then-store under the caller's held
// z.mu, so concurrent calls for the same ID collapse to a single hub instead
// of orphaning one. created reports whether this call stored the hub.
func (z *zeroHub) getOrCreateHubLocked(hubId string, metadata string, isPermanent bool) (hub.Hub, bool, error) {
if existing := z.GetHubById(hubId); existing != nil {
return existing, false, nil
}
Expand Down
38 changes: 38 additions & 0 deletions server/pkg/zerohub/zerohub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,41 @@ func TestGetOrCreateHubReturnsExisting(t *testing.T) {
t.Fatal("expected distinct hubs for distinct IDs")
}
}

// TestSharedCoreBuildHubFailure proves that when buildHub fails (unknown peer
// storage config), the shared getOrCreateHubLocked core surfaces the error and
// stores nothing, through both public methods. This guards the error branch
// that was previously duplicated in each method and now lives once in the core.
func TestSharedCoreBuildHubFailure(t *testing.T) {
zh := &zeroHub{
cfg: &config.Config{
App: config.AppConfig{
PeerStorage: "bogus",
},
},
HubStorage: storage.NewMemoryStorage[hub.Hub](),
}

h, err := zh.CreateHubIfAbsent("x", "meta", false)
if err == nil {
t.Fatalf("CreateHubIfAbsent: got nil error, want unknown peer storage error")
}
if h != nil {
t.Fatalf("CreateHubIfAbsent: got hub %p, want nil on error", h)
}

h, created, err := zh.GetOrCreateHub("x", "meta", false)
if err == nil {
t.Fatalf("GetOrCreateHub: got nil error, want unknown peer storage error")
}
if h != nil {
t.Fatalf("GetOrCreateHub: got hub %p, want nil on error", h)
}
if created {
t.Fatalf("GetOrCreateHub created = true, want false on build error")
}

if got := zh.GetHubById("x"); got != nil {
t.Fatalf("failed build left a hub in storage: %p", got)
}
}
Loading