2019-11-18 16:22:08 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-11-26 11:09:23 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-11-18 16:22:08 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockedRequest struct {
|
2019-11-25 12:47:57 +00:00
|
|
|
msg string
|
|
|
|
name string
|
|
|
|
code codes.Code
|
|
|
|
handler TTLCondition
|
2019-11-18 16:22:08 +00:00
|
|
|
RequestMetaHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMetaRequest(t *testing.T) {
|
|
|
|
tests := []mockedRequest{
|
|
|
|
{
|
|
|
|
name: "direct to ir node",
|
2019-11-25 12:47:57 +00:00
|
|
|
handler: IRNonForwarding(InnerRingNode),
|
2019-11-18 16:22:08 +00:00
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: NonForwardingTTL},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
code: codes.InvalidArgument,
|
|
|
|
msg: ErrIncorrectTTL.Error(),
|
|
|
|
name: "direct to storage node",
|
2019-11-25 12:47:57 +00:00
|
|
|
handler: IRNonForwarding(StorageNode),
|
2019-11-18 16:22:08 +00:00
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: NonForwardingTTL},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
msg: ErrZeroTTL.Error(),
|
|
|
|
code: codes.InvalidArgument,
|
|
|
|
name: "zero ttl",
|
2019-11-25 12:47:57 +00:00
|
|
|
handler: IRNonForwarding(StorageNode),
|
2019-11-18 16:22:08 +00:00
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: ZeroTTL},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "default to ir node",
|
2019-11-25 12:47:57 +00:00
|
|
|
handler: IRNonForwarding(InnerRingNode),
|
2019-11-18 16:22:08 +00:00
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "default to storage node",
|
2019-11-25 12:47:57 +00:00
|
|
|
handler: IRNonForwarding(StorageNode),
|
2019-11-18 16:22:08 +00:00
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
|
|
|
},
|
2019-11-25 12:47:57 +00:00
|
|
|
{
|
|
|
|
msg: "not found",
|
|
|
|
code: codes.NotFound,
|
|
|
|
name: "custom status error",
|
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
|
|
|
handler: func(_ uint32) error { return status.Error(codes.NotFound, "not found") },
|
|
|
|
},
|
2019-11-26 11:09:23 +00:00
|
|
|
{
|
|
|
|
msg: "not found",
|
|
|
|
code: codes.NotFound,
|
|
|
|
name: "custom wrapped status error",
|
|
|
|
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
|
|
|
handler: func(_ uint32) error {
|
|
|
|
err := status.Error(codes.NotFound, "not found")
|
|
|
|
err = errors.Wrap(err, "some error context")
|
|
|
|
err = errors.Wrap(err, "another error context")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
2019-11-18 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tests {
|
|
|
|
tt := tests[i]
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
before := tt.GetTTL()
|
2019-11-25 12:47:57 +00:00
|
|
|
err := ProcessRequestTTL(&tt, tt.handler)
|
2019-11-18 16:22:08 +00:00
|
|
|
if tt.msg != "" {
|
|
|
|
require.Errorf(t, err, tt.msg)
|
|
|
|
|
|
|
|
state, ok := status.FromError(err)
|
|
|
|
require.True(t, ok)
|
2019-11-25 12:47:57 +00:00
|
|
|
require.Equal(t, tt.code, state.Code())
|
|
|
|
require.Equal(t, tt.msg, state.Message())
|
2019-11-18 16:22:08 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEqualf(t, before, tt.GetTTL(), "ttl should be changed: %d vs %d", before, tt.GetTTL())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 09:14:19 +00:00
|
|
|
|
|
|
|
func TestRequestMetaHeader_SetEpoch(t *testing.T) {
|
|
|
|
m := new(ResponseMetaHeader)
|
|
|
|
epoch := uint64(3)
|
|
|
|
m.SetEpoch(epoch)
|
|
|
|
require.Equal(t, epoch, m.GetEpoch())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestMetaHeader_SetVersion(t *testing.T) {
|
|
|
|
m := new(ResponseMetaHeader)
|
|
|
|
version := uint32(3)
|
|
|
|
m.SetVersion(version)
|
|
|
|
require.Equal(t, version, m.GetVersion())
|
|
|
|
}
|