service: ProcessRequestTTL shouldn't change status errors
This commit is contained in:
parent
28628d3b52
commit
5c031fc6b2
2 changed files with 23 additions and 12 deletions
|
@ -101,6 +101,10 @@ func ProcessRequestTTL(req MetaHeader, cond ...TTLCondition) error {
|
||||||
|
|
||||||
// check specific condition:
|
// check specific condition:
|
||||||
if err := cond[i](ttl); err != nil {
|
if err := cond[i](ttl); err != nil {
|
||||||
|
if st, ok := status.FromError(err); ok {
|
||||||
|
return st.Err()
|
||||||
|
}
|
||||||
|
|
||||||
return status.New(codes.InvalidArgument, err.Error()).Err()
|
return status.New(codes.InvalidArgument, err.Error()).Err()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,58 +9,65 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockedRequest struct {
|
type mockedRequest struct {
|
||||||
msg string
|
msg string
|
||||||
name string
|
name string
|
||||||
role NodeRole
|
code codes.Code
|
||||||
code codes.Code
|
handler TTLCondition
|
||||||
RequestMetaHeader
|
RequestMetaHeader
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMetaRequest(t *testing.T) {
|
func TestMetaRequest(t *testing.T) {
|
||||||
tests := []mockedRequest{
|
tests := []mockedRequest{
|
||||||
{
|
{
|
||||||
role: InnerRingNode,
|
|
||||||
name: "direct to ir node",
|
name: "direct to ir node",
|
||||||
|
handler: IRNonForwarding(InnerRingNode),
|
||||||
RequestMetaHeader: RequestMetaHeader{TTL: NonForwardingTTL},
|
RequestMetaHeader: RequestMetaHeader{TTL: NonForwardingTTL},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: StorageNode,
|
|
||||||
code: codes.InvalidArgument,
|
code: codes.InvalidArgument,
|
||||||
msg: ErrIncorrectTTL.Error(),
|
msg: ErrIncorrectTTL.Error(),
|
||||||
name: "direct to storage node",
|
name: "direct to storage node",
|
||||||
|
handler: IRNonForwarding(StorageNode),
|
||||||
RequestMetaHeader: RequestMetaHeader{TTL: NonForwardingTTL},
|
RequestMetaHeader: RequestMetaHeader{TTL: NonForwardingTTL},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: StorageNode,
|
|
||||||
msg: ErrZeroTTL.Error(),
|
msg: ErrZeroTTL.Error(),
|
||||||
code: codes.InvalidArgument,
|
code: codes.InvalidArgument,
|
||||||
name: "zero ttl",
|
name: "zero ttl",
|
||||||
|
handler: IRNonForwarding(StorageNode),
|
||||||
RequestMetaHeader: RequestMetaHeader{TTL: ZeroTTL},
|
RequestMetaHeader: RequestMetaHeader{TTL: ZeroTTL},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: InnerRingNode,
|
|
||||||
name: "default to ir node",
|
name: "default to ir node",
|
||||||
|
handler: IRNonForwarding(InnerRingNode),
|
||||||
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: StorageNode,
|
|
||||||
name: "default to storage node",
|
name: "default to storage node",
|
||||||
|
handler: IRNonForwarding(StorageNode),
|
||||||
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
RequestMetaHeader: RequestMetaHeader{TTL: SingleForwardingTTL},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
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") },
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range tests {
|
for i := range tests {
|
||||||
tt := tests[i]
|
tt := tests[i]
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
before := tt.GetTTL()
|
before := tt.GetTTL()
|
||||||
err := ProcessRequestTTL(&tt, IRNonForwarding(tt.role))
|
err := ProcessRequestTTL(&tt, tt.handler)
|
||||||
if tt.msg != "" {
|
if tt.msg != "" {
|
||||||
require.Errorf(t, err, tt.msg)
|
require.Errorf(t, err, tt.msg)
|
||||||
|
|
||||||
state, ok := status.FromError(err)
|
state, ok := status.FromError(err)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
require.Equal(t, state.Code(), tt.code)
|
require.Equal(t, tt.code, state.Code())
|
||||||
require.Equal(t, state.Message(), tt.msg)
|
require.Equal(t, tt.msg, state.Message())
|
||||||
} else {
|
} else {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.NotEqualf(t, before, tt.GetTTL(), "ttl should be changed: %d vs %d", before, tt.GetTTL())
|
require.NotEqualf(t, before, tt.GetTTL(), "ttl should be changed: %d vs %d", before, tt.GetTTL())
|
||||||
|
|
Loading…
Reference in a new issue