mirror of
https://github.com/status-im/status-go.git
synced 2026-08-31 00:51:12 +00:00
Part of the Go project layout migration, item 27. Pure move plus import-path rewrite across 502 files. No API or behaviour change. `internal/` keeps the messaging application logic unimportable from outside the module, which is what the issue asks for -- status-go is consumed through the C-bindings in mobile/, not as a Go library. Things that had to follow the move, beyond the Go imports: - tools/generate-handlers/template.txt. messenger_handlers.go is generated, and the template hard-codes the imports it emits, so the generated file kept importing protocol/common and failed typecheck. - .gitignore. The ignore rule for that generated file was pinned to the old path; without moving it, a 1486-line generated file starts being tracked. - Makefile: the logosstorage and torrent test targets (both the archive packages and ./protocol itself), the archive README, migration-protocol. - scripts/run_unit_tests.sh, which names the protocol package explicitly to shard its tests. - scripts/cleanup_generated_files.sh and .golangci.yml. scripts/migration_check.sh also needed a fix that is not specific to this move: it validated every file the branch touched under a migration dir against the timestamp naming rule, and a directory rename makes every migration in it look newly added. It now excludes renames, so moving a migration is not mistaken for adding one. refs #7067
96 lines
2.6 KiB
Go
96 lines
2.6 KiB
Go
package protocol
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/zap"
|
|
|
|
messagingtypes "github.com/status-im/status-go/pkg/messaging/types"
|
|
)
|
|
|
|
func TestApplyHistoryWindowFloor(t *testing.T) {
|
|
window := &messagingtypes.HistoryReconcileWindow{
|
|
From: time.Unix(1_000, 0),
|
|
To: time.Unix(1_100, 0),
|
|
}
|
|
|
|
require.Equal(t, uint32(940), applyHistoryWindowFloor(100, true, window),
|
|
"an old cursor should be bounded to the unreliable window with tolerance")
|
|
require.Equal(t, uint32(980), applyHistoryWindowFloor(980, true, window),
|
|
"a newer completeness cursor should remain authoritative")
|
|
require.Equal(t, uint32(100), applyHistoryWindowFloor(100, false, window),
|
|
"an uninitialized topic must retain its initial-history range")
|
|
require.Equal(t, uint32(100), applyHistoryWindowFloor(100, true, nil))
|
|
}
|
|
|
|
func TestTranslateHistoryReconcileWindowUsesMessengerClock(t *testing.T) {
|
|
window := messagingtypes.HistoryReconcileWindow{
|
|
From: time.Unix(100, 0),
|
|
To: time.Unix(200, 0),
|
|
}
|
|
|
|
got := translateHistoryReconcileWindow(
|
|
window,
|
|
time.Unix(250, 0),
|
|
time.Unix(1_000, 0),
|
|
)
|
|
require.Equal(t, time.Unix(850, 0), got.From)
|
|
require.Equal(t, time.Unix(950, 0), got.To)
|
|
}
|
|
|
|
func TestCoalesceHistoricSyncRequests(t *testing.T) {
|
|
at := func(seconds int64) time.Time { return time.Unix(seconds, 0) }
|
|
requests := []historicSyncRequest{
|
|
{From: at(100), To: at(200)},
|
|
{From: at(150), To: at(250)},
|
|
{From: at(400), To: at(450)},
|
|
{To: at(300)},
|
|
{To: at(350)},
|
|
}
|
|
|
|
got := coalesceHistoricSyncRequests(requests)
|
|
require.Equal(t, []historicSyncRequest{
|
|
{To: at(350)},
|
|
{From: at(400), To: at(450)},
|
|
}, got)
|
|
}
|
|
|
|
func TestCoalesceHistoricSyncRequestsKeepsAdjacentReliableGap(t *testing.T) {
|
|
first := historicSyncRequest{From: time.Unix(100, 0), To: time.Unix(200, 0)}
|
|
second := historicSyncRequest{From: time.Unix(201, 0), To: time.Unix(300, 0)}
|
|
|
|
require.Equal(t, []historicSyncRequest{first, second},
|
|
coalesceHistoricSyncRequests([]historicSyncRequest{first, second}))
|
|
}
|
|
|
|
func TestEnqueueHistoricSyncConcurrent(t *testing.T) {
|
|
m := &Messenger{
|
|
logger: zap.NewNop(),
|
|
historicSyncTrigger: make(chan struct{}, 1),
|
|
}
|
|
from := time.Unix(100, 0)
|
|
|
|
var wg sync.WaitGroup
|
|
for i := 1; i <= 100; i++ {
|
|
wg.Add(1)
|
|
go func(offset int) {
|
|
defer wg.Done()
|
|
m.enqueueHistoricSync(historicSyncRequest{
|
|
From: from,
|
|
To: from.Add(time.Duration(offset) * time.Second),
|
|
})
|
|
}(i)
|
|
}
|
|
wg.Wait()
|
|
|
|
m.historicSyncQueueMu.Lock()
|
|
defer m.historicSyncQueueMu.Unlock()
|
|
require.Equal(t, []historicSyncRequest{{
|
|
From: from,
|
|
To: from.Add(100 * time.Second),
|
|
}}, m.historicSyncQueue)
|
|
}
|