Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new 'conn' command with option to close connections
- Loading branch information
1 parent
d90a54b
commit eb379fc
Showing
13 changed files
with
289 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"os" | ||
|
||
rabtap "github.com/jandelgado/rabtap/pkg" | ||
) | ||
|
||
func cmdConnClose(apiURI, connName, reason string, tlsConfig *tls.Config) error { | ||
client := rabtap.NewRabbitHTTPClient(apiURI, tlsConfig) | ||
err := client.CloseConnection(connName, reason) | ||
failOnError(err, fmt.Sprintf("close connection '%s'", connName), os.Exit) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (C) 2017 Jan Delgado | ||
// +build integration | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"testing" | ||
"time" | ||
|
||
rabtap "github.com/jandelgado/rabtap/pkg" | ||
"github.com/jandelgado/rabtap/pkg/testcommon" | ||
"github.com/streadway/amqp" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func findClosedConnName(connectionsBefore []rabtap.RabbitConnection, | ||
connectionsAfter []rabtap.RabbitConnection) string { | ||
// given to lists of connections, find the first connection by name which | ||
// is in the first, but not in the second list. | ||
for _, ca := range connectionsAfter { | ||
found := false | ||
for _, cb := range connectionsBefore { | ||
if ca.Name == cb.Name { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
return ca.Name | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func TestCmdCloseConnection(t *testing.T) { | ||
|
||
uri := testcommon.IntegrationAPIURIFromEnv() | ||
client := rabtap.NewRabbitHTTPClient(uri, &tls.Config{}) | ||
|
||
// we can not get the name of a connection through the API of the AMQP client. So | ||
// we figure out the connections name by comparing the list of active | ||
// connection before and after we created our test connection. Therefore, | ||
// make sure this test runs isolated on the broker. | ||
connsBefore, err := client.Connections() | ||
require.Nil(t, err) | ||
|
||
// start the test connection to be terminated | ||
conn, _ := testcommon.IntegrationTestConnection(t, "", "", 0, false) | ||
|
||
// it takes a few seconds for the new connection to show up in the REST API | ||
time.Sleep(time.Second * 5) | ||
|
||
connsAfter, err := client.Connections() | ||
require.Nil(t, err) | ||
|
||
// we add a notification callback and expect the cb to be called | ||
// when we close the connection via the API | ||
errorChan := make(chan *amqp.Error) | ||
conn.NotifyClose(errorChan) | ||
|
||
connToClose := findClosedConnName(connsBefore, connsAfter) | ||
require.NotEqual(t, "", connToClose) | ||
|
||
// now close the newly created connection. TODO handle potential | ||
// call to failOnError in cmdConnClose | ||
err = cmdConnClose(uri, connToClose, "some reason", &tls.Config{}) | ||
require.Nil(t, err) | ||
|
||
// ... and make sure it gets closed, notified by a message on the errorChan | ||
connClosed := false | ||
select { | ||
case <-errorChan: | ||
connClosed = true | ||
case <-time.After(time.Second * 2): | ||
assert.Fail(t, "did not receive notification within expected time") | ||
} | ||
assert.True(t, connClosed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.