Skip to content

Commit

Permalink
feat: subscription expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Oct 4, 2023
1 parent 014bfd6 commit 87993a6
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 161 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.20.4
go-version: 1.21.0

- name: Install Protoc
uses: arduino/setup-protoc@v1
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ func main() {
subData := subscription.Data{
Description: "my subscription",
Enabled: true,
Expires: time.Now().Add(24 * time.Hour), // optional, subscription will be treated as disabled after it expires
Condition: condition.NewBuilder().
AttributeKey("tags").
AnyOfWords("SpaceX").
Expand Down
6 changes: 5 additions & 1 deletion api/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func TestClient_CreateSubscription(t *testing.T) {
ctx := context.TODO()
subData := subscription.Data{
Description: c.descr,
Expires: time.Now(),
}
id, err := cl.CreateSubscription(ctx, "user0", subData)
assert.Equal(t, c.id, id)
Expand All @@ -413,6 +414,7 @@ func TestClient_ReadSubscription(t *testing.T) {
subData: subscription.Data{
Description: "my subscription",
Enabled: true,
Expires: time.Date(2023, 10, 4, 11, 44, 55, 0, time.UTC),
Condition: condition.
NewBuilder().
Any([]condition.Condition{
Expand Down Expand Up @@ -496,7 +498,9 @@ func TestClient_UpdateSubscriptionMetadata(t *testing.T) {
svcSubs: c.svcSubs,
}
ctx := context.TODO()
err := cl.UpdateSubscription(ctx, "user0", c.subId, subscription.Data{})
err := cl.UpdateSubscription(ctx, "user0", c.subId, subscription.Data{
Expires: time.Now(),
})
assert.ErrorIs(t, err, c.err)
assert.Nil(t, cl.Close())
})
Expand Down
3 changes: 3 additions & 0 deletions api/grpc/subscriptions/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)

type clientMock struct{}
Expand Down Expand Up @@ -44,6 +46,7 @@ func (cm clientMock) Read(ctx context.Context, req *ReadRequest, opts ...grpc.Ca
default:
resp.Description = "subscription"
resp.Enabled = true
resp.Expires = timestamppb.New(time.Date(2023, 10, 4, 11, 44, 55, 0, time.UTC))
resp.Cond = &Condition{
Cond: &Condition_Gc{
Gc: &GroupCondition{
Expand Down
6 changes: 6 additions & 0 deletions api/grpc/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/awakari/client-sdk-go/model/subscription/condition"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
)

type Service interface {
Expand Down Expand Up @@ -59,6 +60,7 @@ func (svc service) Create(ctx context.Context, userId string, subData subscripti
Cond: encodeCondition(subData.Condition),
Description: subData.Description,
Enabled: subData.Enabled,
Expires: timestamppb.New(subData.Expires.UTC()),
}
var resp *CreateResponse
resp, err = svc.client.Create(ctx, &req)
Expand All @@ -81,6 +83,9 @@ func (svc service) Read(ctx context.Context, userId, subId string) (subData subs
subData.Condition, err = decodeCondition(resp.Cond)
subData.Description = resp.Description
subData.Enabled = resp.Enabled
if resp.Expires != nil {
subData.Expires = resp.Expires.AsTime()
}
}
return
}
Expand All @@ -91,6 +96,7 @@ func (svc service) Update(ctx context.Context, userId, subId string, data subscr
Id: subId,
Description: data.Description,
Enabled: data.Enabled,
Expires: timestamppb.New(data.Expires.UTC()),
}
_, err = svc.client.Update(ctx, &req)
err = decodeError(err)
Expand Down
355 changes: 198 additions & 157 deletions api/grpc/subscriptions/service.pb.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions api/grpc/subscriptions/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ syntax = "proto3";
package awakari.subscriptions.proxy;

option go_package = "./api/grpc/subscriptions";
import "google/protobuf/timestamp.proto";

service Service {

Expand All @@ -19,6 +20,7 @@ message CreateRequest {
string description = 1;
bool enabled = 2;
Condition cond = 3;
google.protobuf.Timestamp expires = 4;
}

// Condition represents the Subscription routing Condition data that is immutable once Subscription is created.
Expand Down Expand Up @@ -79,6 +81,7 @@ message ReadResponse {
string description = 1;
bool enabled = 2;
Condition cond = 3;
google.protobuf.Timestamp expires = 4;
}

// Update
Expand All @@ -87,6 +90,7 @@ message UpdateRequest {
string id = 1;
string description = 2;
bool enabled = 3;
google.protobuf.Timestamp expires = 4;
}

message UpdateResponse {
Expand Down
2 changes: 2 additions & 0 deletions api/grpc/subscriptions/service_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/awakari/client-sdk-go/api/grpc/limits"
"github.com/awakari/client-sdk-go/model/subscription"
"github.com/awakari/client-sdk-go/model/subscription/condition"
"time"
)

type serviceMock struct {
Expand Down Expand Up @@ -44,6 +45,7 @@ func (sm serviceMock) Read(ctx context.Context, userId, subId string) (subData s
default:
subData.Description = "my subscription"
subData.Enabled = true
subData.Expires = time.Date(2023, 10, 4, 11, 44, 55, 0, time.UTC)
subData.Condition = condition.
NewBuilder().
Any([]condition.Condition{
Expand Down
13 changes: 13 additions & 0 deletions api/grpc/subscriptions/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func TestService_Create(t *testing.T) {
},
err: limits.ErrReached,
},
"ok with expiration": {
req: subscription.Data{
Description: "my subscription",
Expires: time.Now(),
Condition: condition.NewTextCondition(
condition.NewKeyCondition(condition.NewCondition(false), "key0"),
"ok", false,
),
},
},
}
//
for k, c := range cases {
Expand Down Expand Up @@ -134,6 +144,7 @@ func TestService_Read(t *testing.T) {
sd: subscription.Data{
Description: "subscription",
Enabled: true,
Expires: time.Date(2023, 10, 4, 11, 44, 55, 0, time.UTC),
Condition: condition.
NewBuilder().
Any(
Expand Down Expand Up @@ -165,6 +176,7 @@ func TestService_Read(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, c.sd.Description, sd.Description)
assert.Equal(t, c.sd.Enabled, sd.Enabled)
assert.Equal(t, c.sd.Expires, sd.Expires)
assert.True(t, conditionsDataEqual(c.sd.Condition, sd.Condition))
} else {
assert.ErrorIs(t, err, c.err)
Expand All @@ -191,6 +203,7 @@ func TestService_Update(t *testing.T) {
sd: subscription.Data{
Description: "my subscription",
Enabled: false,
Expires: time.Now(),
},
},
"fail": {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/awakari/client-sdk-go

go 1.20
go 1.21

require (
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.14.0
Expand Down
8 changes: 7 additions & 1 deletion model/subscription/data.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package subscription

import "github.com/awakari/client-sdk-go/model/subscription/condition"
import (
"github.com/awakari/client-sdk-go/model/subscription/condition"
"time"
)

type Data struct {

Expand All @@ -13,4 +16,7 @@ type Data struct {

// Enabled defines whether subscription is active and may be used to deliver a message.
Enabled bool

// Expires defines a deadline when subscription becomes disabled regardless the Enabled value.
Expires time.Time
}

0 comments on commit 87993a6

Please sign in to comment.