mattermost/server/public/utils/page_test.go
Alejandro García Montoro 553f99612e
MM-60441: Re-index public channels when a user joins a team (#33400)
* Index all public channels when a user joins a team

* Precompute team members for indexChannelsForTeam

* Refactor RequestContextWithMaster to store package

This way, we can import it from both the sqlstore and the searchlayer
packages. The alternative for this is duplicating the code in those two
packages, but that will *not* work:

The context package expects custom types for the keys stored in it, so
that different packages never clash with each other when trying to
register a new key. See the docs for the WithValue function:
https://pkg.go.dev/context#WithValue

If we try to duplicate the storeContextKey type in both the sqlstore and
searchlayer packages, although they *look* the same, they are not, and
HasMaster will fail to get the value of the storeContextKey(useMaster)
key if it's from the other package.

* Use master in call to GetTeamMembersForChannel

In GetTeamMembersForChannel, use the DB from the newly passed context,
which will be the receiving context everywhere except in the call done
from indexChannelsForTeam, to avoid the read after write issue when
saving a team member.

* Fix GetPublicChannelsForTeam paging

We were using the page and perPage arguments as is in the call to
GetPublicChannelsForTeam, but that function expects and offset and a
limit as understood by SQL. Although perPage and limit are
interchangeable, offset is not equal to page, but to page * perPage.

* Add a synchronous bulk indexer for Opensearch

* Implement Opensearch's SyncBulkIndexChannels

* Add a synchronous bulk indexer for Elasticsearch

* Implement Elasticsearch's SynkBulkIndexChannels

* Test SyncBulkIndexChannels

* make mocks

* Bulk index channels on indexChannelsForTeam

* Handle error from SyncBulkIndexChannels

* Fix style

* Revert indexChannelWithTeamMembers refactor

* Remove defensive code on sync bulk processor

* Revert "Add a synchronous bulk indexer for Opensearch"

This reverts commit bfe4671d96.

* Revert "Add a synchronous bulk indexer for Elasticsearch"

This reverts commit 6643ae3f30.

* Refactor bulk indexers with a common interface

* Test all the different implementations

Assisted by Claude

* Remove debug statements

* Refactor common code into _stop

* Rename getUserIDsFor{,Private}Channel

* Wrap error

* Make perPage a const

* Fix typos

* Call GetTeamsForUser only if needed

* Differentiate errors for sync/async processors

---------

Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
2025-08-25 19:28:19 +02:00

69 lines
1.4 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package utils
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestPager(t *testing.T) {
tests := []struct {
name string
fetch func(page int) ([]int, error)
perPage int
expected []int
expectErr bool
}{
{
name: "successful fetch",
fetch: func(page int) ([]int, error) {
if page > 2 {
return nil, nil
}
return []int{page*10 + 1, page*10 + 2, page*10 + 3}, nil
},
perPage: 3,
expected: []int{1, 2, 3, 11, 12, 13, 21, 22, 23},
},
{
name: "fetch with error",
fetch: func(page int) ([]int, error) {
if page == 1 {
return nil, errors.New("fetch error")
}
return []int{page*10 + 1, page*10 + 2, page*10 + 3}, nil
},
perPage: 3,
expected: []int{1, 2, 3},
expectErr: true,
},
{
name: "fetch with fewer items than perPage",
fetch: func(page int) ([]int, error) {
if page > 0 {
return []int{11, 12}, nil
}
return []int{page*10 + 1, page*10 + 2, page*10 + 3}, nil
},
perPage: 3,
expected: []int{1, 2, 3, 11, 12},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := Pager(tt.fetch, tt.perPage)
if tt.expectErr {
assert.Error(t, err)
assert.Equal(t, tt.expected, result)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}