Skip to content

Commit

Permalink
reduce cyclomatic complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
KazuhitoNakamura committed Jun 28, 2023
1 parent 17abea4 commit fa8cd95
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 55 deletions.
18 changes: 12 additions & 6 deletions rocro.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ inspecode:
- "**/*_test.go"
runtime:
go: 1.20
env:
GO111MODULE: on
cpd: default
dependency-check:
machine:
cpu: 2
errcheck: default
ineffassign: default
gofmt: default
goimports: default
golint: default
misspell: default
gosec: default
errcheck: default
staticcheck: default
gosec:
machine:
cpu: 2
go-test:
options:
- -cover
misspell: default
staticcheck:
machine:
cpu: 2
127 changes: 78 additions & 49 deletions test/EventApp/appsync_eventapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,39 @@ func main() {
client := appsync.NewClient(appsync.NewGraphQLClient(graphql.NewClient(*url)), opt)

log.Println("mutation createEvent()")
event := createEvent(client)

log.Println("subscription subscribeToEventComments()")
ch := make(chan *graphql.Response)
defer close(ch)
var s subscriber
switch *protocol {
case "mqtt":
s = mqttSubscribeToEventComments(client, event, ch)
case "graphql-ws":
s = wsSubscribeToEventComments(*url, sOpt, event, ch)
default:
log.Fatalln("unsupported protocol: " + *protocol)
}
if err := s.Start(); err != nil {
log.Fatalln(err)
}
defer s.Stop()

log.Println("mutation commentOnEvent()")
commentOnEvent(client, event)
msg, ok := <-ch
if !ok {
log.Fatal("ch has been closed.")
}
log.Println("comment received")
_, _ = pp.Println(msg)

log.Println("mutation deleteEvent()")
deleteEvent(client, event)
}

func createEvent(c *appsync.Client) *event {
mutation := `
mutation {
createEvent(name: "name", when: "when", where: "where", description: "description") {
Expand All @@ -75,7 +108,7 @@ mutation {
description
}
}`
res, err := client.Post(graphql.PostRequest{
res, err := c.Post(graphql.PostRequest{
Query: mutation,
})
if err != nil {
Expand All @@ -87,8 +120,10 @@ mutation {
if err := res.DataAs(ev); err != nil {
log.Fatalln(err)
}
return ev
}

log.Println("subscription subscribeToEventComments()")
func mqttSubscribeToEventComments(c *appsync.Client, e *event, ch chan *graphql.Response) subscriber {
subscription := fmt.Sprintf(`
subscription {
subscribeToEventComments(eventId: "%s"){
Expand All @@ -97,75 +132,69 @@ subscription {
content
createdAt
}
}`, ev.ID)

var s subscriber
}`, e.ID)
subreq := graphql.PostRequest{
Query: subscription,
}
res, err := c.Post(subreq)
if err != nil {
log.Fatalln(err)
}
_, _ = pp.Println(res)

ch := make(chan *graphql.Response)
defer close(ch)
ext, err := appsync.NewExtensions(res)
if err != nil {
log.Fatalln(err)
}

switch *protocol {
case "mqtt":
res, err = client.Post(subreq)
if err != nil {
log.Fatalln(err)
}
_, _ = pp.Println(res)
return appsync.NewSubscriber(*ext,
func(r *graphql.Response) { ch <- r },
func(err error) { log.Println(err) })

ext, err := appsync.NewExtensions(res)
if err != nil {
log.Fatalln(err)
}
}

s = appsync.NewSubscriber(*ext,
func(r *graphql.Response) { ch <- r },
func(err error) { log.Println(err) })
case "graphql-ws":
realtime := strings.Replace(strings.Replace(*url, "https", "wss", 1), "appsync-api", "appsync-realtime-api", 1)
s = appsync.NewPureWebSocketSubscriber(realtime, subreq,
func(r *graphql.Response) { ch <- r },
func(err error) { log.Println(err) },
sOpt,
)
default:
log.Fatalln("unsupported protocol: " + *protocol)
func wsSubscribeToEventComments(url string, opt appsync.PureWebSocketSubscriberOption, e *event, ch chan *graphql.Response) subscriber {
subscription := fmt.Sprintf(`
subscription {
subscribeToEventComments(eventId: "%s"){
eventId
commentId
content
createdAt
}

if err := s.Start(); err != nil {
log.Fatalln(err)
}`, e.ID)
subreq := graphql.PostRequest{
Query: subscription,
}
defer s.Stop()
realtime := strings.Replace(strings.Replace(url, "https", "wss", 1), "appsync-api", "appsync-realtime-api", 1)
return appsync.NewPureWebSocketSubscriber(realtime, subreq,
func(r *graphql.Response) { ch <- r },
func(err error) { log.Println(err) },
opt,
)
}

log.Println("mutation commentOnEvent()")
mutation = fmt.Sprintf(`
func commentOnEvent(c *appsync.Client, e *event) {
mutation := fmt.Sprintf(`
mutation {
commentOnEvent(eventId: "%s", content: "content", createdAt: "%s"){
eventId
commentId
content
createdAt
}
}`, ev.ID, time.Now().String())
res, err = client.Post(graphql.PostRequest{
}`, e.ID, time.Now().String())
res, err := c.Post(graphql.PostRequest{
Query: mutation,
})
if err != nil {
log.Fatalln(err)
}
_, _ = pp.Println(res)
}

msg, ok := <-ch
if !ok {
log.Fatal("ch has been closed.")
}
log.Println("comment received")
_, _ = pp.Println(msg)

log.Println("mutation deleteEvent()")
mutation = fmt.Sprintf(`
func deleteEvent(c *appsync.Client, e *event) {
mutation := fmt.Sprintf(`
mutation {
deleteEvent(id: "%s"){
id
Expand All @@ -174,8 +203,8 @@ mutation {
where
description
}
}`, ev.ID)
res, err = client.Post(graphql.PostRequest{
}`, e.ID)
res, err := c.Post(graphql.PostRequest{
Query: mutation,
})
if err != nil {
Expand Down

0 comments on commit fa8cd95

Please sign in to comment.