mirror of
https://github.com/lucacasonato/mqtt.git
synced 2025-09-26 19:01:12 +08:00
Added disconnect function
This commit is contained in:
18
.github/workflows/tests.yml
vendored
18
.github/workflows/tests.yml
vendored
@@ -4,7 +4,7 @@ on: [push]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test & Coverage
|
||||
name: Tests
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.13
|
||||
@@ -16,7 +16,21 @@ jobs:
|
||||
- name: Get dependencies
|
||||
run: go mod download
|
||||
- name: Run tests & coverage
|
||||
run: go test -race -coverprofile=coverage.txt -covermode=atomic
|
||||
run: go test -race
|
||||
coverage:
|
||||
name: Coverage
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.13
|
||||
uses: actions/setup-go@v1
|
||||
with:
|
||||
go-version: 1.13
|
||||
- name: Check out code
|
||||
uses: actions/checkout@v1
|
||||
- name: Get dependencies
|
||||
run: go mod download
|
||||
- name: Run tests & coverage
|
||||
run: go test -coverprofile=coverage.txt -covermode=atomic
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v1.0.3
|
||||
with:
|
||||
|
5
mqtt.go
5
mqtt.go
@@ -81,3 +81,8 @@ func (c *Client) Connect(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Disconnect will immediately close the conenction with the mqtt servers
|
||||
func (c *Client) DisconnectImmediately() {
|
||||
c.client.Disconnect(0)
|
||||
}
|
||||
|
21
mqtt_test.go
21
mqtt_test.go
@@ -189,3 +189,24 @@ func TestConnectFailed(t *testing.T) {
|
||||
t.Fatal("connect should have failed")
|
||||
}
|
||||
}
|
||||
|
||||
// check that a client gets created and a client id is not changed when it is set
|
||||
func TestDisconnectImmediately(t *testing.T) {
|
||||
client, err := mqtt.NewClient(mqtt.ClientOptions{
|
||||
Servers: []string{
|
||||
"tcp://test.mosquitto.org:1883",
|
||||
},
|
||||
AutoReconnect: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal("err should be nil")
|
||||
}
|
||||
if client == nil {
|
||||
t.Fatal("client should not be nil")
|
||||
}
|
||||
err = client.Connect(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal("connect should not have failed")
|
||||
}
|
||||
client.DisconnectImmediately()
|
||||
}
|
||||
|
Reference in New Issue
Block a user