-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Avoid using single topic from multiple tests * Fix KeepAlive error handling test stability
- Loading branch information
Showing
5 changed files
with
170 additions
and
64 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,64 @@ | ||
// Copyright 2019 The mqtt-go authors. | ||
// | ||
// Licensed 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. | ||
|
||
package filteredpipe | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
// DetectAndClosePipe creates pair of filtered pipe. | ||
// Handler is called on each Write and determine to close the connection. | ||
func DetectAndClosePipe(h0, h1 func([]byte) bool) (io.ReadWriteCloser, io.ReadWriteCloser) { | ||
ch0 := make(chan []byte, 1000) | ||
ch1 := make(chan []byte, 1000) | ||
return &detectAndCloseConn{ | ||
baseFilterConn: &baseFilterConn{ | ||
rCh: ch0, | ||
wCh: ch1, | ||
handler: h0, | ||
closed: make(chan struct{}), | ||
}, | ||
}, &detectAndCloseConn{ | ||
baseFilterConn: &baseFilterConn{ | ||
rCh: ch1, | ||
wCh: ch0, | ||
handler: h1, | ||
closed: make(chan struct{}), | ||
}, | ||
} | ||
} | ||
|
||
type detectAndCloseConn struct { | ||
*baseFilterConn | ||
} | ||
|
||
func (c *detectAndCloseConn) Write(data []byte) (n int, err error) { | ||
if c.handler(data) { | ||
c.closeOnce.Do(func() { close(c.closed) }) | ||
return 0, io.ErrClosedPipe | ||
} | ||
select { | ||
case <-c.closed: | ||
return 0, io.ErrClosedPipe | ||
default: | ||
} | ||
cp := append([]byte{}, data...) | ||
select { | ||
case <-c.closed: | ||
return 0, io.ErrClosedPipe | ||
case c.wCh <- cp: | ||
} | ||
return len(cp), nil | ||
} |
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,70 @@ | ||
// Copyright 2019 The mqtt-go authors. | ||
// | ||
// Licensed 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. | ||
|
||
package filteredpipe | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
) | ||
|
||
// DetectAndDropPipe creates pair of filtered pipe. | ||
// Handler is called on each Write and determine to drop the remaining data. | ||
func DetectAndDropPipe(h0, h1 func([]byte) bool) (io.ReadWriteCloser, io.ReadWriteCloser) { | ||
ch0 := make(chan []byte, 1000) | ||
ch1 := make(chan []byte, 1000) | ||
return &detectAndDropConn{ | ||
baseFilterConn: &baseFilterConn{ | ||
rCh: ch0, | ||
wCh: ch1, | ||
handler: h0, | ||
closed: make(chan struct{}), | ||
}, | ||
dropping: make(chan struct{}), | ||
}, &detectAndDropConn{ | ||
baseFilterConn: &baseFilterConn{ | ||
rCh: ch1, | ||
wCh: ch0, | ||
handler: h1, | ||
closed: make(chan struct{}), | ||
}, | ||
dropping: make(chan struct{}), | ||
} | ||
} | ||
|
||
type detectAndDropConn struct { | ||
*baseFilterConn | ||
dropping chan struct{} | ||
dropOnce sync.Once | ||
} | ||
|
||
func (c *detectAndDropConn) Write(data []byte) (n int, err error) { | ||
if c.handler(data) { | ||
c.dropOnce.Do(func() { close(c.dropping) }) | ||
} | ||
select { | ||
case <-c.closed: | ||
return 0, io.ErrClosedPipe | ||
case <-c.dropping: | ||
return len(data), nil | ||
default: | ||
} | ||
cp := append([]byte{}, data...) | ||
select { | ||
case <-c.closed: | ||
return 0, io.ErrClosedPipe | ||
case c.wCh <- cp: | ||
} | ||
return len(cp), nil | ||
} |
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