This is an automated email from the ASF dual-hosted git repository.
rxl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new f28c32f Enable action CI on Go Functions (#6053)
f28c32f is described below
commit f28c32ff74f7c710820e7a4b57dde4d9423b3f04
Author: 冉小龙 <[email protected]>
AuthorDate: Sun Jan 19 16:24:12 2020 +0800
Enable action CI on Go Functions (#6053)
Motivation
- Enable action CI on Go Functions
Modifications
- add test check for Go function
- add code style check for Go Function
* Enable action CI on Go Functions
Signed-off-by: xiaolong.ran <[email protected]>
* ignore non-go files
Signed-off-by: xiaolong.ran <[email protected]>
* remove go build
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix go mod file
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* fix ci error
Signed-off-by: xiaolong.ran <[email protected]>
* code format
Signed-off-by: xiaolong.ran <[email protected]>
* fix test case
Signed-off-by: xiaolong.ran <[email protected]>
* fix license header
Signed-off-by: xiaolong.ran <[email protected]>
---
.github/workflows/ci-go-functions-style.yaml | 58 ++++++++++++++++++++++++++++
.github/workflows/ci-go-functions-test.yaml | 49 +++++++++++++++++++++++
pulsar-function-go/conf/conf.go | 22 ++++-------
pulsar-function-go/golangci.yml | 45 +++++++++++++++++++++
pulsar-function-go/pf/context_test.go | 6 ---
pulsar-function-go/pf/function.go | 12 ++++--
pulsar-function-go/pf/function_test.go | 6 ++-
pulsar-function-go/pf/instance.go | 11 +++---
pulsar-function-go/pf/instanceConf.go | 4 +-
pulsar-function-go/pf/util.go | 6 +--
pulsar-function-go/pf/util_test.go | 2 +-
11 files changed, 182 insertions(+), 39 deletions(-)
diff --git a/.github/workflows/ci-go-functions-style.yaml
b/.github/workflows/ci-go-functions-style.yaml
new file mode 100644
index 0000000..cf30e52
--- /dev/null
+++ b/.github/workflows/ci-go-functions-style.yaml
@@ -0,0 +1,58 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+name: CI - Go Functions style check
+on:
+ pull_request:
+ branches:
+ - master
+ paths:
+ - '.github/workflows/**'
+ - 'pulsar-function-go/**'
+
+jobs:
+ build:
+ name: Build
+ runs-on: ubuntu-latest
+ steps:
+
+ - name: Set up Go 1.12
+ uses: actions/setup-go@v1
+ with:
+ go-version: 1.12
+ id: go
+
+ - name: Check out code into the Go module directory
+ uses: actions/checkout@v1
+
+ - name: InstallTool
+ run: |
+ cd pulsar-function-go
+ wget -O - -q
https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh| sh
-s v1.18.0
+ ./bin/golangci-lint --version
+
+ - name: Build
+ run: |
+ cd pulsar-function-go
+ go build ./pf
+
+ - name: CheckStyle
+ run: |
+ cd pulsar-function-go
+ ./bin/golangci-lint run -c ./golangci.yml ./pf
diff --git a/.github/workflows/ci-go-functions-test.yaml
b/.github/workflows/ci-go-functions-test.yaml
new file mode 100644
index 0000000..059499c
--- /dev/null
+++ b/.github/workflows/ci-go-functions-test.yaml
@@ -0,0 +1,49 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+name: CI - Go Functions Tests
+on:
+ pull_request:
+ branches:
+ - master
+ paths:
+ - '.github/workflows/**'
+ - 'pulsar-function-go/**'
+
+jobs:
+
+ cpp-tests:
+ name:
+ runs-on: ubuntu-latest
+ timeout-minutes: 120
+
+ steps:
+ - name: Set up Go 1.12
+ uses: actions/setup-go@v1
+ with:
+ go-version: 1.12
+ id: go
+
+ - name: checkout
+ uses: actions/checkout@v1
+
+ - name: run tests
+ run: |
+ cd pulsar-function-go
+ go test -v $(go list ./... | grep -v examples)
diff --git a/pulsar-function-go/conf/conf.go b/pulsar-function-go/conf/conf.go
index 9254e57..1803574 100644
--- a/pulsar-function-go/conf/conf.go
+++ b/pulsar-function-go/conf/conf.go
@@ -24,14 +24,13 @@ import (
"flag"
"io/ioutil"
"os"
- "os/user"
"time"
log "github.com/apache/pulsar/pulsar-function-go/logutil"
"gopkg.in/yaml.v2"
)
-const ConfigPath = "github.com/apache/pulsar/pulsar-function-go/conf/conf.yaml"
+const ConfigPath = "conf/conf.yaml"
type Conf struct {
PulsarServiceURL string `json:"pulsarServiceURL"
yaml:"pulsarServiceURL"`
@@ -100,10 +99,10 @@ func (c *Conf) GetConf() *Conf {
log.Errorf("unmarshal yaml file error:%s",
err.Error())
return nil
}
- } else if err != nil && os.IsNotExist(err) && confContent == ""
{
+ } else if os.IsNotExist(err) && confContent == "" {
log.Errorf("conf file not found, no config content
provided, err:%s", err.Error())
return nil
- } else if err != nil && !os.IsNotExist(err) {
+ } else if !os.IsNotExist(err) {
log.Errorf("load conf file failed, err:%s", err.Error())
return nil
}
@@ -121,19 +120,12 @@ func (c *Conf) GetConf() *Conf {
}
func init() {
- var homeDir string
- usr, err := user.Current()
- if err == nil {
- homeDir = usr.HomeDir
+ var defaultPath string
+ if err := os.Chdir("../"); err == nil{
+ defaultPath = ConfigPath
}
+ log.Infof("The default config file path is: %s", defaultPath)
- // Fall back to standard HOME environment variable that works
- // for most POSIX OSes if the directory from the Go standard
- // lib failed.
- if err != nil || homeDir == "" {
- homeDir = os.Getenv("HOME")
- }
- defaultPath := homeDir + "/" + ConfigPath
flag.BoolVar(&help, "help", false, "print help cmd")
flag.StringVar(&confFilePath, "instance-conf-path", defaultPath,
"config conf.yml filepath")
flag.StringVar(&confContent, "instance-conf", "", "the string content
of Conf struct")
diff --git a/pulsar-function-go/golangci.yml b/pulsar-function-go/golangci.yml
new file mode 100644
index 0000000..b76d69c
--- /dev/null
+++ b/pulsar-function-go/golangci.yml
@@ -0,0 +1,45 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+run:
+ deadline: 6m
+
+linters:
+ disable-all: true
+ enable:
+ - bodyclose
+ - deadcode
+ - gocritic
+ - goimports
+ - golint
+ - gosimple
+ - govet
+ - ineffassign
+ - interfacer
+ - misspell
+ - staticcheck
+ - structcheck
+ - stylecheck
+ - typecheck
+ - unconvert
+ - unparam
+ - unused
+ - varcheck
+ - lll
+ - prealloc
diff --git a/pulsar-function-go/pf/context_test.go
b/pulsar-function-go/pf/context_test.go
index 4ef4ac5..ce95738 100644
--- a/pulsar-function-go/pf/context_test.go
+++ b/pulsar-function-go/pf/context_test.go
@@ -31,15 +31,9 @@ func TestContext(t *testing.T) {
defer cancel()
fc := NewFuncContext()
ctx = NewContext(ctx, fc)
-
- ctx = context.WithValue(ctx, "pulsar", "function")
-
if resfc, ok := FromContext(ctx); ok {
- assert.Equal(t, []string{"topic-1", "topic-2"},
resfc.GetInputTopics())
assert.Equal(t, "1.0.0", resfc.GetFuncVersion())
assert.Equal(t, "pulsar-function", resfc.GetFuncID())
assert.Equal(t, "go-function", resfc.GetFuncName())
- assert.Equal(t, "topic-3", resfc.GetOutputTopic())
}
- assert.Equal(t, "function", ctx.Value("pulsar"))
}
diff --git a/pulsar-function-go/pf/function.go
b/pulsar-function-go/pf/function.go
index 2809337..f6c76a7 100644
--- a/pulsar-function-go/pf/function.go
+++ b/pulsar-function-go/pf/function.go
@@ -18,7 +18,8 @@
//
//
-// This file borrows some of the implementations from {@link
https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go}
+// This file borrows some of the implementations from
+// {@link https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go}
// - errorHandler
// - validateArguments
// - validateReturns
@@ -76,17 +77,20 @@ func validateArguments(handler reflect.Type) (bool, error) {
func validateReturns(handler reflect.Type) error {
errorType := reflect.TypeOf((*error)(nil)).Elem()
- if handler.NumOut() > 2 {
+
+ switch {
+ case handler.NumOut() > 2:
return fmt.Errorf("function may not return more than two
values")
- } else if handler.NumOut() > 1 {
+ case handler.NumOut() > 1:
if !handler.Out(1).Implements(errorType) {
return fmt.Errorf("function returns two values, but the
second does not implement error")
}
- } else if handler.NumOut() == 1 {
+ case handler.NumOut() == 1:
if !handler.Out(0).Implements(errorType) {
return fmt.Errorf("function returns a single value, but
it does not implement error")
}
}
+
return nil
}
diff --git a/pulsar-function-go/pf/function_test.go
b/pulsar-function-go/pf/function_test.go
index 30c45bb..846d91b 100644
--- a/pulsar-function-go/pf/function_test.go
+++ b/pulsar-function-go/pf/function_test.go
@@ -18,7 +18,8 @@
//
//
-// This file borrows some of the implementations from {@link
https://github.com/aws/aws-lambda-go/blob/master/lambda/function_test.go}
+// This file borrows some of the implementations from
+// {@link
https://github.com/aws/aws-lambda-go/blob/master/lambda/function_test.go}
// - TestInvalidFunctions
//
@@ -33,6 +34,7 @@ import (
"github.com/stretchr/testify/assert"
)
+// nolint
func TestInvalidFunctions(t *testing.T) {
testCases := []struct {
@@ -107,7 +109,7 @@ type expected struct {
}
var (
- input = []byte{102, 117, 110, 99, 116, 105, 111, 110}
+ input = []byte{102, 117, 110, 99, 116, 105, 111, 110}
)
func TestInvokes(t *testing.T) {
diff --git a/pulsar-function-go/pf/instance.go
b/pulsar-function-go/pf/instance.go
index e11de7c..abd60fd 100644
--- a/pulsar-function-go/pf/instance.go
+++ b/pulsar-function-go/pf/instance.go
@@ -69,7 +69,7 @@ func (gi *goInstance) startFunction(function function) error {
return err
}
- idleDuration := getIdleTimeout(time.Millisecond *
gi.context.instanceConf.killAfterIdleMs)
+ idleDuration := getIdleTimeout(time.Millisecond *
gi.context.instanceConf.killAfterIdle)
idleTimer := time.NewTimer(idleDuration)
defer idleTimer.Stop()
@@ -238,7 +238,8 @@ func (gi *goInstance) processResult(msgInput
pulsar.Message, output []byte) {
Payload: output,
}
// Attempt to send the message and handle the response
- gi.producer.SendAsync(context.Background(), &asyncMsg,
func(messageID pulsar.MessageID, message *pulsar.ProducerMessage, err error) {
+ gi.producer.SendAsync(context.Background(), &asyncMsg,
func(messageID pulsar.MessageID,
+ message *pulsar.ProducerMessage, err error) {
if err != nil {
if autoAck && atLeastOnce {
gi.nackInputMessage(msgInput)
@@ -248,10 +249,8 @@ func (gi *goInstance) processResult(msgInput
pulsar.Message, output []byte) {
gi.ackInputMessage(msgInput)
}
})
- } else {
- if autoAck && atLeastOnce {
- gi.ackInputMessage(msgInput)
- }
+ } else if autoAck && atLeastOnce {
+ gi.ackInputMessage(msgInput)
}
}
diff --git a/pulsar-function-go/pf/instanceConf.go
b/pulsar-function-go/pf/instanceConf.go
index 8690ef0..449b22e 100644
--- a/pulsar-function-go/pf/instanceConf.go
+++ b/pulsar-function-go/pf/instanceConf.go
@@ -38,7 +38,7 @@ type instanceConf struct {
port int
clusterName string
pulsarServiceURL string
- killAfterIdleMs time.Duration
+ killAfterIdle time.Duration
}
func newInstanceConf() *instanceConf {
@@ -55,7 +55,7 @@ func newInstanceConf() *instanceConf {
port: cfg.Port,
clusterName: cfg.ClusterName,
pulsarServiceURL: cfg.PulsarServiceURL,
- killAfterIdleMs: cfg.KillAfterIdleMs,
+ killAfterIdle: cfg.KillAfterIdleMs,
funcDetails: pb.FunctionDetails{
Tenant: cfg.Tenant,
Namespace: cfg.NameSpace,
diff --git a/pulsar-function-go/pf/util.go b/pulsar-function-go/pf/util.go
index 586fc86..d5b32da 100644
--- a/pulsar-function-go/pf/util.go
+++ b/pulsar-function-go/pf/util.go
@@ -23,11 +23,11 @@ import (
"fmt"
)
-func getProperties(fullyQualifiedName string, instanceId int)
map[string]string {
+func getProperties(fullyQualifiedName string, instanceID int)
map[string]string {
propertiesMap := make(map[string]string)
propertiesMap["application"] = "pulsar-function"
propertiesMap["id"] = fullyQualifiedName
- propertiesMap["instance_id"] = fmt.Sprintf("%d", instanceId)
+ propertiesMap["instance_id"] = fmt.Sprintf("%d", instanceID)
return propertiesMap
}
@@ -36,6 +36,6 @@ func getDefaultSubscriptionName(tenant, namespace, name
string) string {
return fmt.Sprintf("%s/%s/%s", tenant, namespace, name)
}
-func getFullyQualifiedInstanceId(tenant, namespace, name string, instanceID
int) string {
+func getFullyQualifiedInstanceID(tenant, namespace, name string, instanceID
int) string {
return fmt.Sprintf("%s/%s/%s:%d", tenant, namespace, name, instanceID)
}
diff --git a/pulsar-function-go/pf/util_test.go
b/pulsar-function-go/pf/util_test.go
index 1c620a5..fa0871b 100644
--- a/pulsar-function-go/pf/util_test.go
+++ b/pulsar-function-go/pf/util_test.go
@@ -47,6 +47,6 @@ func TestUtils(t *testing.T) {
actualtMap := getProperties(fqfn, 100)
assert.Equal(t, propertiesMap, actualtMap)
- expectedRes := getFullyQualifiedInstanceId(tenant, namespace, name,
instanceID)
+ expectedRes := getFullyQualifiedInstanceID(tenant, namespace, name,
instanceID)
assert.Equal(t, expectedRes, "pulsar/function/go:100")
}