From 37ce3403571c63a6a0a1ea91630aeb0ee0b7fae3 Mon Sep 17 00:00:00 2001 From: Luca Casonato Date: Sun, 20 Oct 2019 00:13:39 +0200 Subject: [PATCH] Added disconnect function --- .github/workflows/tests.yml | 18 ++++++++++++++++-- mqtt.go | 5 +++++ mqtt_test.go | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 52dbfab..d433761 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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: diff --git a/mqtt.go b/mqtt.go index 3fcd0e8..a7f5b6c 100644 --- a/mqtt.go +++ b/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) +} diff --git a/mqtt_test.go b/mqtt_test.go index a42b17a..cf64ca3 100644 --- a/mqtt_test.go +++ b/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() +}