Skip to content

Commit

Permalink
fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
nad2000 committed Aug 15, 2019
1 parent b24f6d8 commit 39ee95d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 47 deletions.
4 changes: 2 additions & 2 deletions handler/batch_test.sh
@@ -1,7 +1,7 @@
URL=${1:-https://415mdw939a.execute-api.ap-southeast-2.amazonaws.com/prod/v1/enqueue}
URL=${1:-https://415mdw939a.execute-api.ap-southeast-2.amazonaws.com/dev/v1/call}

for id in 484378182 477579437 208013283 987654321 8524255 350622514 4306445 ; do
curl -v -H "Content-Type: application/json" -d "{\"subject\":${id}}" $URL
curl -v -H "Content-Type: application/json" -d "{\"subject\":\"${id}\"}" $URL
done


Expand Down
4 changes: 2 additions & 2 deletions handler/batch_test_parallel.sh
@@ -1,6 +1,6 @@
export URL=${1:-https://415mdw939a.execute-api.ap-southeast-2.amazonaws.com/prod/v1/enqueue}
export URL=${1:-https://415mdw939a.execute-api.ap-southeast-2.amazonaws.com/dev/v1/call}

xargs -P 100 -L 1 -i -t curl -v -H "Content-Type: application/json" -d "{\"subject\":{}}" $URL <<EOF
xargs -P 100 -L 1 -i -t curl -H "Content-Type: application/json" -d "{\"subject\":\"{}\"}" $URL <<EOF
4306445
484378182
477579437
Expand Down
25 changes: 20 additions & 5 deletions handler/common.go
Expand Up @@ -17,35 +17,50 @@ import (
"go.uber.org/zap/zapcore"
)

const (
taskFilenamePrefix = "UOA-OH-INTEGRATION-TASK-"
defaultTaskRetentionMin = 5.0
defaultBatchSize = 12
)

var (
api Client
batchSize = defaultBatchSize
counter int
gotAccessTokenWG sync.WaitGroup
log *zap.SugaredLogger
logger *zap.Logger
loggingLevel zapcore.Level
oh Client
taskCreatedAt time.Time
taskID int
taskRecordCount int
taskRecordCountMutex sync.Mutex
taskRetentionMin = defaultTaskRetentionMin
updateOrcidWG sync.WaitGroup
verbose bool
wg sync.WaitGroup

loggingLevel zapcore.Level
logger *zap.Logger
log *zap.SugaredLogger
// for testing/mocking
logFatal func(args ...interface{})
)

const taskFilenamePrefix = "UOA-OH-INTEGRATION-TASK-"

var (
// APIBaseURL is the UoA API base URL
APIBaseURL = "https://api.dev.auckland.ac.nz/service"
// OHBaseURL is the ORCID Hub API base URL
OHBaseURL = "https://dev.orcidhub.org.nz"
)

func getenvInt(key string, defaultValue int) int {
if value := os.Getenv(key); value != "" {
if intValue, err := strconv.Atoi(value); err == nil {
return intValue
}
}
return defaultValue
}

func init() {
godotenv.Load()

Expand Down
42 changes: 24 additions & 18 deletions handler/handler_test.go
Expand Up @@ -25,6 +25,9 @@ func init() {
flag.BoolVar(&verbose, "verbose", false, "Print out the received responses.")
flag.BoolVar(&live, "live", false, "Run with the DEV/SANDBOX APIs.")
flag.Parse()

taskRetentionMin = 1
batchSize = 1
}

func isValidUUID(u string) bool {
Expand Down Expand Up @@ -193,6 +196,9 @@ func testIdentityAPICient(t *testing.T) {
assert.Equal(t, 0, id.ID)
malformatResponse = false

err = c.do("POST", "identity/integrations/v3/identity/rcir178", nil, &id)
assert.Nil(t, err)

}

func testEmploymentAPICient(t *testing.T) {
Expand Down Expand Up @@ -482,13 +488,13 @@ func testProcessEmpUpdate(t *testing.T) {
taskRecordCount = 0
_, err = (&Event{
Records: []events.SQSMessage{
{Body: `{"subject":484378182}`},
{Body: `{"subject":477579437}`},
{Body: `{"subject":208013283}`},
{Body: `{"subject":987654321}`},
{Body: `{"subject":8524255}`},
{Body: `{"subject":350622514}`},
{Body: `{"subject":4306445}`},
{Body: `{"subject":"484378182"}`},
{Body: `{"subject":"477579437"}`},
{Body: `{"subject":"208013283"}`},
{Body: `{"subject":"987654321"}`},
{Body: `{"subject":"8524255"}`},
{Body: `{"subject":"350622514"}`},
{Body: `{"subject":"4306445"}`},
},
}).handle()
assert.True(t, taskRecordCount > 0, "The number of records should be > 0.")
Expand All @@ -503,18 +509,18 @@ func testProcessMixed(t *testing.T) {
taskRecordCount = 0
_, err = (&Event{
Records: []events.SQSMessage{
{Body: `{"subject":484378182}`},
{Body: `{"subject":477579437}`},
{Body: `{"subject":"484378182"}`},
{Body: `{"subject":"477579437"}`},
{Body: `{
"orcid": "0000-0001-8228-7153",
"url": "https://sandbox.orcid.org/0000-0001-8228-7153",
"type": "CREATED", "updated-at": "2019-07-25T02:05:32",
"email": "rad42@mailinator.com",
"eppn": "rcir178@auckland.ac.nz"
}`},
{Body: `{"subject":208013283}`},
{Body: `{"subject":66666666}`},
{Body: `{"subject":77777777}`},
{Body: `{"subject":"208013283"}`},
{Body: `{"subject":"66666666"}`},
{Body: `{"subject":"77777777"}`},
{Body: `{
"orcid": "0000-0001-6666-7153",
"url": "https://sandbox.orcid.org/0000-0001-6666-7153",
Expand All @@ -531,8 +537,8 @@ func testProcessMixed(t *testing.T) {
"email": "dthn7777mailinator.com",
"eppn": "dthn7777auckland.ac.nz"
}`},
{Body: `{"subject":987654321}`},
{Body: `{"subject":8524255}`},
{Body: `{"subject":"987654321"}`},
{Body: `{"subject":"8524255"}`},
{Body: `{
"orcid": "0000-0001-8228-7153",
"url": "https://sandbox.orcid.org/0000-0001-8228-7153",
Expand All @@ -541,8 +547,8 @@ func testProcessMixed(t *testing.T) {
"email": "rad42@mailinator.com",
"eppn": "rcir178@auckland.ac.nz"
}`},
{Body: `{"subject":350622514}`},
{Body: `{"subject":4306445}`},
{Body: `{"subject":"350622514"}`},
{Body: `{"subject":"4306445"}`},
},
}).handle()
assert.True(t, taskRecordCount == 3, "The number of records should be 3, got: %d.", taskRecordCount)
Expand All @@ -552,8 +558,8 @@ func testProcessMixed(t *testing.T) {
assert.NotNil(t, err)
_, err = (&Event{
Records: []events.SQSMessage{
{Body: `{"subject":484378182}`},
{Body: `{"subject":477579437}`},
{Body: `{"subject":"484378182"}`},
{Body: `{"subject":"477579437"}`},
},
}).handle()
assert.NotNil(t, err)
Expand Down
31 changes: 16 additions & 15 deletions handler/main.go
Expand Up @@ -16,31 +16,32 @@ import (

var lambdazapper *lambdazap.LambdaLogContext

// Response - lambda response
type Response struct {
Message string `json:"message,omitempty"`
Retry bool `json:"retry"`
}
// // Response - lambda response
// type Response struct {
// Message string `json:"message,omitempty"`
// Retry bool `json:"retry"`
// }

// HandleRequest handle "AWS lambda" request with a single event message or
// a batch of event messages.
func HandleRequest(ctx context.Context, e Event) (Response, error) {
func HandleRequest(ctx context.Context, e Event) (message string, err error) {

defer func() {
logger.Sync()
wg.Wait()
logger.Sync()
}()

message, err := e.handle()
if err != nil {
if message != "" {
message += ": " + err.Error()
} else {
message = err.Error()
}
}
return Response{Message: message, Retry: err != nil}, nil
message, err = e.handle()
// if err != nil {
// if message != "" {
// message += ": " + err.Error()
// } else {
// message = err.Error()
// }
// }
// return Response{Message: message, Retry: err != nil}, err
return message, err
}

func main() {
Expand Down
5 changes: 0 additions & 5 deletions handler/task.go
Expand Up @@ -9,11 +9,6 @@ import (

var taskSetUpWG sync.WaitGroup

const (
taskRetentionMin = 5
batchSize = 12
)

// Task - ORCID Hub affiliation registration batch task
type Task struct {
ID int `json:"id,omitempty"`
Expand Down

0 comments on commit 39ee95d

Please sign in to comment.