Skip to content

Commit

Permalink
Enables ability to find things in the list.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Tucker committed Jan 17, 2020
1 parent 0523c1c commit 3fb276a
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 49 deletions.
141 changes: 107 additions & 34 deletions proto/recordadder.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion proto/recordadder.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ message AddRecordResponse {
int64 expected_addition_date = 1;
}

message ListQueueRequest {}

message ListQueueResponse {
repeated AddRecordRequest requests = 1;
}

service AddRecordService {
rpc AddRecord(AddRecordRequest) returns (AddRecordResponse) {};
rpc Test(AddRecordRequest) returns (AddRecordResponse) {};
rpc ListQueue(ListQueueRequest) returns (ListQueueResponse) {};
}
14 changes: 6 additions & 8 deletions recordadder_cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"log"
"os"
"strconv"
"time"

"github.com/brotherlogic/goserver/utils"
Expand All @@ -15,14 +14,15 @@ import (

//Needed to pull in gzip encoding init
_ "google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/resolver"
)

func init() {
resolver.Register(&utils.DiscoveryClientResolverBuilder{})
}

func main() {
host, port, err := utils.Resolve("recordadder", "recordadder-cli")
if err != nil {
log.Fatalf("Unable to reach recordadder: %v", err)
}
conn, err := grpc.Dial(host+":"+strconv.Itoa(int(port)), grpc.WithInsecure())
conn, err := grpc.Dial("discovery:///recordadder", grpc.WithInsecure(), grpc.WithBalancerName("my_pick_first"))
if err != nil {
log.Fatalf("Unable to dial: %v", err)
}
Expand All @@ -33,8 +33,6 @@ func main() {
defer cancel()

switch os.Args[1] {
case "bump":
client.Test(ctx, &pb.AddRecordRequest{})
case "add":
addFlags := flag.NewFlagSet("AddRecords", flag.ExitOnError)
var id = addFlags.Int("id", -1, "Id of the record to add")
Expand Down
13 changes: 9 additions & 4 deletions recordadderapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ func (s *Server) AddRecord(ctx context.Context, req *pb.AddRecordRequest) (*pb.A
return &pb.AddRecordResponse{ExpectedAdditionDate: time.Now().Add(time.Hour * time.Duration((24 * len(queue.Requests)))).Unix()}, err
}

// Test test function
func (s *Server) Test(ctx context.Context, req *pb.AddRecordRequest) (*pb.AddRecordResponse, error) {
time.Sleep(time.Minute)
return nil, nil
//ListQueue lists the entries in the queue
func (s *Server) ListQueue(ctx context.Context, req *pb.ListQueueRequest) (*pb.ListQueueResponse, error) {
data, _, err := s.KSclient.Read(ctx, QUEUE, &pb.Queue{})
if err != nil {
return nil, err
}
queue := data.(*pb.Queue)

return &pb.ListQueueResponse{Requests: queue.GetRequests()}, nil
}
29 changes: 27 additions & 2 deletions recordadderapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ func TestAddRequestFail(t *testing.T) {
}
}

func TestCallTest(t *testing.T) {
func TestListQueueFail(t *testing.T) {
s := InitTestServer()
s.Test(context.Background(), &pb.AddRecordRequest{})
s.GoServer.KSclient.Fail = true

val, err := s.ListQueue(context.Background(), &pb.ListQueueRequest{})
if err == nil {
t.Errorf("Add Record with failing read did not fail: %v", val)
}
}

func TestDoubleAddRequest(t *testing.T) {
Expand All @@ -61,3 +66,23 @@ func TestDoubleAddRequest(t *testing.T) {
t.Fatalf("Double addition should have failed: %v", val)
}
}

func TestListQueue(t *testing.T) {
s := InitTestServer()

_, err := s.AddRecord(context.Background(), &pb.AddRecordRequest{Id: 123})
if err != nil {
t.Fatalf("Add Record failed: %v", err)
}

q, err := s.ListQueue(context.Background(), &pb.ListQueueRequest{})

if err != nil {
t.Fatalf("Error listing queue: %v", err)
}

if len(q.GetRequests()) != 1 {
t.Errorf("Wrong number of requests in queue: %v", q.GetRequests())
}

}

0 comments on commit 3fb276a

Please sign in to comment.