-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reactor): add IReactorProcess and IReactorSocket support
- Loading branch information
Showing
16 changed files
with
162 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# 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. | ||
|
||
from collections.abc import Mapping, Sequence | ||
from typing import AnyStr | ||
|
||
from twisted.internet.interfaces import IProcessProtocol, IProcessTransport | ||
from twisted.internet.task import Clock | ||
from twisted.internet.testing import MemoryReactor as TwistedMemoryReactor | ||
|
||
|
||
class MemoryReactor(TwistedMemoryReactor): | ||
"""A drop-in replacement for Twisted's own MemoryReactor that adds support for IReactorProcess.""" | ||
|
||
def run(self) -> None: | ||
""" | ||
We have to override TwistedMemoryReactor.run() because the original Twisted implementation weirdly calls stop() | ||
inside run(), and we need the reactor running during our tests. | ||
""" | ||
self.running = True | ||
|
||
def spawnProcess( | ||
self, | ||
processProtocol: IProcessProtocol, | ||
executable: bytes | str, | ||
args: Sequence[bytes | str], | ||
env: Mapping[AnyStr, AnyStr] | None = None, | ||
path: bytes | str | None = None, | ||
uid: int | None = None, | ||
gid: int | None = None, | ||
usePTY: bool = False, | ||
childFDs: Mapping[int, int | str] | None = None, | ||
) -> IProcessTransport: | ||
raise NotImplementedError | ||
|
||
|
||
class MemoryReactorClock(MemoryReactor, Clock): | ||
"""A drop-in replacement for Twisted's own MemoryReactorClock that adds support for IReactorProcess.""" | ||
|
||
def __init__(self) -> None: | ||
MemoryReactor.__init__(self) | ||
Clock.__init__(self) |
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,38 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# 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. | ||
|
||
from collections.abc import Mapping, Sequence | ||
from typing import AnyStr, Protocol | ||
|
||
from twisted.internet.interfaces import IProcessProtocol, IProcessTransport, IReactorProcess | ||
from zope.interface import implementer | ||
|
||
|
||
@implementer(IReactorProcess) | ||
class ReactorProcessProtocol(Protocol): | ||
"""A Python protocol that stubs Twisted's IReactorProcess interface.""" | ||
|
||
def spawnProcess( | ||
self, | ||
processProtocol: IProcessProtocol, | ||
executable: bytes | str, | ||
args: Sequence[bytes | str], | ||
env: Mapping[AnyStr, AnyStr] | None = None, | ||
path: bytes | str | None = None, | ||
uid: int | None = None, | ||
gid: int | None = None, | ||
usePTY: bool = False, | ||
childFDs: Mapping[int, int | str] | None = None, | ||
) -> IProcessTransport: | ||
... |
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,45 @@ | ||
# Copyright 2024 Hathor Labs | ||
# | ||
# 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. | ||
|
||
from socket import AddressFamily | ||
from typing import Protocol | ||
|
||
from twisted.internet.interfaces import IListeningPort, IReactorSocket | ||
from twisted.internet.protocol import DatagramProtocol, ServerFactory | ||
from zope.interface import implementer | ||
|
||
|
||
@implementer(IReactorSocket) | ||
class ReactorSocketProtocol(Protocol): | ||
"""A Python protocol that stubs Twisted's IReactorSocket interface.""" | ||
|
||
def adoptStreamPort( | ||
self, | ||
fileDescriptor: int, | ||
addressFamily: AddressFamily, | ||
factory: ServerFactory, | ||
) -> IListeningPort: | ||
... | ||
|
||
def adoptStreamConnection(self, fileDescriptor: int, addressFamily: AddressFamily, factory: ServerFactory) -> None: | ||
... | ||
|
||
def adoptDatagramPort( | ||
self, | ||
fileDescriptor: int, | ||
addressFamily: AddressFamily, | ||
protocol: DatagramProtocol, | ||
maxPacketSize: int, | ||
) -> IListeningPort: | ||
... |
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
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 was deleted.
Oops, something went wrong.
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