mirror of
https://github.com/hanzoai/o11y.git
synced 2026-10-15 12:58:33 +00:00
The five claim verbs need the address an operation will answer at, which is a root joined with a path. They used to ask the target for its prefix through a struct written to answer with one — and that struct existed for a second reason that has since gone: an operation with no declared id took a shape-derived one qualified by the prefix of the occurrence it answered under, so a plain group would have renamed every undeclared id. All 319 operations state their own id now, so a declared id survives composition verbatim and the group is just a group. The roots are this package's own constants, so each group carries the one it was made with and nothing can disagree about the address. The escape hatches name their whole address and register through Raw, which says which method each one answers. The test that measured the gap reached zero and said so; it now holds the property from the other side, that no operation goes undeclared. Co-authored-by: Hanzo Dev <[email protected]>
103 lines
3.2 KiB
Go
103 lines
3.2 KiB
Go
package queryprogress
|
|
|
|
import (
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
|
|
datastore "github.com/hanzo-ds/go"
|
|
"github.com/hanzoai/o11y/pkg/query-service/model"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestQueryProgressTracking(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
tracker := NewQueryProgressTracker(slog.Default())
|
|
|
|
testQueryId := "test-query"
|
|
|
|
testProgress := &datastore.Progress{}
|
|
err := tracker.ReportQueryProgress(testQueryId, testProgress)
|
|
require.NotNil(err, "shouldn't be able to report query progress before query has been started")
|
|
require.Equal(err.Type(), model.ErrorNotFound)
|
|
|
|
ch, unsubscribe, err := tracker.SubscribeToQueryProgress(testQueryId)
|
|
require.NotNil(err, "shouldn't be able to subscribe for progress updates before query has been started")
|
|
require.Equal(err.Type(), model.ErrorNotFound)
|
|
require.Nil(ch)
|
|
require.Nil(unsubscribe)
|
|
|
|
reportQueryFinished, err := tracker.ReportQueryStarted(testQueryId)
|
|
require.Nil(err, "should be able to report start of a query to be tracked")
|
|
|
|
testProgress1 := &datastore.Progress{
|
|
Rows: 10,
|
|
Bytes: 20,
|
|
TotalRows: 100,
|
|
Elapsed: 20 * time.Millisecond,
|
|
}
|
|
err = tracker.ReportQueryProgress(testQueryId, testProgress1)
|
|
require.Nil(err, "should be able to report progress after query has started")
|
|
|
|
ch, unsubscribe, err = tracker.SubscribeToQueryProgress(testQueryId)
|
|
require.Nil(err, "should be able to subscribe to query progress updates after query started")
|
|
require.NotNil(ch)
|
|
require.NotNil(unsubscribe)
|
|
|
|
expectedProgress := model.QueryProgress{}
|
|
updateQueryProgress(&expectedProgress, testProgress1)
|
|
require.Equal(expectedProgress.ReadRows, testProgress1.Rows)
|
|
select {
|
|
case qp := <-ch:
|
|
require.Equal(qp, expectedProgress)
|
|
default:
|
|
require.Fail("should receive latest query progress state immediately after subscription")
|
|
}
|
|
select {
|
|
case _ = <-ch:
|
|
require.Fail("should have had only one pending update at this point")
|
|
default:
|
|
}
|
|
|
|
testProgress2 := &datastore.Progress{
|
|
Rows: 20,
|
|
Bytes: 40,
|
|
TotalRows: 100,
|
|
Elapsed: 40 * time.Millisecond,
|
|
}
|
|
err = tracker.ReportQueryProgress(testQueryId, testProgress2)
|
|
require.Nil(err, "should be able to report progress multiple times while query is in progress")
|
|
|
|
updateQueryProgress(&expectedProgress, testProgress2)
|
|
select {
|
|
case qp := <-ch:
|
|
require.Equal(qp, expectedProgress)
|
|
default:
|
|
require.Fail("should receive updates whenever new progress updates get reported to tracker")
|
|
}
|
|
select {
|
|
case _ = <-ch:
|
|
require.Fail("should have had only one pending update at this point")
|
|
default:
|
|
}
|
|
|
|
reportQueryFinished()
|
|
select {
|
|
case _, isSubscriptionChannelOpen := <-ch:
|
|
require.False(isSubscriptionChannelOpen, "subscription channels should get closed after query finishes")
|
|
default:
|
|
require.Fail("subscription channels should get closed after query finishes")
|
|
}
|
|
|
|
err = tracker.ReportQueryProgress(testQueryId, testProgress)
|
|
require.NotNil(err, "shouldn't be able to report query progress after query has finished")
|
|
require.Equal(err.Type(), model.ErrorNotFound)
|
|
|
|
ch, unsubscribe, err = tracker.SubscribeToQueryProgress(testQueryId)
|
|
require.NotNil(err, "shouldn't be able to subscribe for progress updates after query has finished")
|
|
require.Equal(err.Type(), model.ErrorNotFound)
|
|
require.Nil(ch)
|
|
require.Nil(unsubscribe)
|
|
}
|