From 63cb08704128b75d435a75153a8b2788dd0872f7 Mon Sep 17 00:00:00 2001 From: Nicholas Molnar <65710+neekolas@users.noreply.github.com> Date: Mon, 20 May 2024 08:27:36 -0700 Subject: [PATCH] Fix issue with loop iterator (#390) ## tl;dr - Fixes a [loop iterator](https://mohamed-abdel-mohaimen.medium.com/using-loop-iterator-variable-issues-in-golang-b76c17fb71b6) issue. My absolute least-favourite design quirk of Go. We should also think about upgrading to [1.22](https://go.dev/blog/loopvar-preview), which finally lets you do away with this behaviour. ## What this fixes Previously, when querying `GetInboxIds` all results would be set to the last inbox ID. --- pkg/mls/store/store.go | 7 ++++--- pkg/mls/store/store_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/mls/store/store.go b/pkg/mls/store/store.go index b94f9d4f..67e915e3 100644 --- a/pkg/mls/store/store.go +++ b/pkg/mls/store/store.go @@ -87,9 +87,10 @@ func (s *Store) GetInboxIds(ctx context.Context, req *identity.GetInboxIdsReques resp := identity.GetInboxIdsResponse_Response{} resp.Address = address - for _, log_entry := range addressLogEntries { - if log_entry.Address == address { - resp.InboxId = &log_entry.InboxID + for _, logEntry := range addressLogEntries { + if logEntry.Address == address { + inboxId := logEntry.InboxID + resp.InboxId = &inboxId } } out[index] = &resp diff --git a/pkg/mls/store/store_test.go b/pkg/mls/store/store_test.go index e4eb3bcb..d5103161 100644 --- a/pkg/mls/store/store_test.go +++ b/pkg/mls/store/store_test.go @@ -69,6 +69,31 @@ func TestInboxIds(t *testing.T) { require.Equal(t, "inbox2", *resp.Responses[1].InboxId) } +func TestMultipleInboxIds(t *testing.T) { + store, cleanup := NewTestStore(t) + defer cleanup() + ctx := context.Background() + + _, err := store.queries.InsertAddressLog(ctx, queries.InsertAddressLogParams{Address: "address_1", InboxID: "inbox_1", AssociationSequenceID: sql.NullInt64{Valid: true, Int64: 1}, RevocationSequenceID: sql.NullInt64{Valid: false}}) + require.NoError(t, err) + _, err = store.queries.InsertAddressLog(ctx, queries.InsertAddressLogParams{Address: "address_2", InboxID: "inbox_2", AssociationSequenceID: sql.NullInt64{Valid: true, Int64: 2}, RevocationSequenceID: sql.NullInt64{Valid: false}}) + require.NoError(t, err) + + reqs := make([]*identity.GetInboxIdsRequest_Request, 0) + reqs = append(reqs, &identity.GetInboxIdsRequest_Request{ + Address: "address_1", + }) + reqs = append(reqs, &identity.GetInboxIdsRequest_Request{ + Address: "address_2", + }) + req := &identity.GetInboxIdsRequest{ + Requests: reqs, + } + resp, _ := store.GetInboxIds(context.Background(), req) + require.Equal(t, "inbox_1", *resp.Responses[0].InboxId) + require.Equal(t, "inbox_2", *resp.Responses[1].InboxId) +} + func TestCreateInstallation(t *testing.T) { store, cleanup := NewTestStore(t) defer cleanup()