ipfs-cluster/pintracker/optracker/operation_test.go
Hector Sanjuan de40b2cd23 pintracker: metrics: convert pinning/queued/error metrics to gauges
We were currently tracking this metrics as a counter (SUM). The number is
good, but conceptually this is more a gauge (LastValue), given it can go down.

Thus we switch it by tracking the aggregation numbers directy in the operation
tracker.
2022-04-26 15:13:35 +02:00

54 lines
987 B
Go

package optracker
import (
"context"
"errors"
"testing"
"time"
"github.com/ipfs/ipfs-cluster/api"
"github.com/ipfs/ipfs-cluster/test"
)
func TestOperation(t *testing.T) {
tim := time.Now().Add(-2 * time.Second)
op := newOperation(context.Background(), api.PinCid(test.Cid1), OperationUnpin, PhaseQueued, nil)
if !op.Cid().Equals(test.Cid1) {
t.Error("bad cid")
}
if op.Phase() != PhaseQueued {
t.Error("bad phase")
}
op.SetError(errors.New("fake error"))
if op.Error() != "fake error" {
t.Error("bad error")
}
op.SetPhase(PhaseInProgress)
if op.Phase() != PhaseInProgress {
t.Error("bad phase")
}
if op.Type() != OperationUnpin {
t.Error("bad type")
}
if !op.Timestamp().After(tim) {
t.Error("bad timestamp")
}
if op.Cancelled() {
t.Error("should not be cancelled")
}
op.Cancel()
if !op.Cancelled() {
t.Error("should be cancelled")
}
if op.ToTrackerStatus() != api.TrackerStatusUnpinning {
t.Error("should be in unpin error")
}
}