Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #574 from GilGil1/master
Custom open connection for different network types. This allows the user to provide a custom function which will be used to open the network connection enabling support for edge cases not supported by the inbuilt function.
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
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,64 @@ | ||
/* | ||
* Copyright (c) 2021 IBM Corp and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* Seth Hoenig | ||
* Allan Stockdill-Mander | ||
* Mike Robertson | ||
* Matt Brittan | ||
*/ | ||
package mqtt | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"strings" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCustomConnectionFunction(t *testing.T) { | ||
// Set netpipe to emu | ||
netClient, netServer := net.Pipe() | ||
defer netClient.Close() | ||
defer netServer.Close() | ||
var firstMessage = "" | ||
go func() { | ||
// read first message only | ||
bytes := make([]byte, 1024) | ||
n, err := netServer.Read(bytes) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
firstMessage = string(bytes[:n]) | ||
}() | ||
// Set custom network connection function and client connect | ||
var customConnectionFunc OpenConnectionFunc = func(uri *url.URL, options ClientOptions) (net.Conn, error) { | ||
return netClient, nil | ||
} | ||
options := &ClientOptions{ | ||
CustomOpenConnectionFn: customConnectionFunc, | ||
} | ||
brokerAddr := netServer.LocalAddr().Network() | ||
options.AddBroker(brokerAddr) | ||
client := NewClient(options) | ||
|
||
// Try to connect using custom function, wait for 2 seconds, to pass MQTT first message | ||
if token := client.Connect(); token.WaitTimeout(2*time.Second) && token.Error() != nil { | ||
t.Errorf("%v", token.Error()) | ||
} | ||
|
||
// Analyze first message sent by client and received by the server | ||
if len(firstMessage) <= 0 || !strings.Contains(firstMessage, "MQTT") { | ||
t.Error("no message recieved on connect") | ||
} | ||
} |
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,39 @@ | ||
/* | ||
* Copyright (c) 2021 IBM Corp and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.php. | ||
* | ||
* Contributors: | ||
* Seth Hoenig | ||
* Allan Stockdill-Mander | ||
* Mike Robertson | ||
* Måns Ansgariusson | ||
*/ | ||
|
||
// Portions copyright © 2018 TIBCO Software Inc. | ||
package mqtt | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/url" | ||
"testing" | ||
) | ||
|
||
func TestSetCustomConnectionOptions(t *testing.T) { | ||
var customConnectionFunc OpenConnectionFunc = func(uri *url.URL, options ClientOptions) (net.Conn, error) { | ||
return nil, fmt.Errorf("not implemented open connection func") | ||
} | ||
options := &ClientOptions{} | ||
options = options.SetCustomOpenConectionFn(customConnectionFunc) | ||
if options.CustomOpenConnectionFn == nil { | ||
t.Error("custom open connection function cannot be set") | ||
} | ||
} |