<commit_before># No shebang line, this module is meant to be imported
#
# Copyright 2014 <NAME>
#
# 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.

import os
from collections import namedtuple

import psutil
from twisted.internet import reactor
from twisted.internet.defer import Deferred, DeferredList
from twisted.internet.error import ProcessTerminated
from twisted.internet.protocol import ProcessProtocol as _ProcessProtocol

from pyfarm.agent.testutil import TestCase
from pyfarm.jobtypes.core.process import ReplaceEnvironment, ProcessProtocol

DummyInputs = namedtuple("DummyInputs", ("task", ))


class FakeJobType(object):
    def __init__(self, stdout=None, stderr=None):
        self.started = Deferred()
        self.stopped = Deferred()
        self.stdout = stdout
        self.stderr = stderr

    def process_started(self, protocol):
        self.started.callback(protocol)

    def process_stopped(self, protocol, reason):
        self.stopped.callback((protocol, reason))

    def received_stdout(self, protocol, data):
        if self.stdout is not None:
            self.stdout(protocol, data)

    def received_stderr(self, protocol, data):
        if self.stderr is not None:
            self.stderr(protocol, data)


class TestProcessBase(TestCase):
    def _launch_python(self, jobtype, script="i = 42"):
        protocol = ProcessProtocol(jobtype, *[None] * 6)
        reactor.spawnProcess(
            protocol, "python", ["python", "-c", script])
        return protocol


class TestProtocol(TestProcessBase):
    def test_subclass(self):
        protocol = ProcessProtocol(*[None] * 7)
        self.assertIsInstance(protocol, _ProcessProtocol)

    def test_pid(self):
        fake_jobtype = FakeJobType()
        protocol = self._launch_python(fake_jobtype)<commit_msg>Don't instance ProcessProtocol with 7 args<commit_after># No shebang line, this module is meant to be imported
#
# Copyright 2014 <NAME>
#
# 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.

import os
from collections import namedtuple

import psutil
from twisted.internet import reactor
from twisted.internet.defer import Deferred, DeferredList
from twisted.internet.error import ProcessTerminated
from twisted.internet.protocol import ProcessProtocol as _ProcessProtocol

from pyfarm.agent.testutil import TestCase
from pyfarm.jobtypes.core.process import ReplaceEnvironment, ProcessProtocol

DummyInputs = namedtuple("DummyInputs", ("task", ))


class FakeJobType(object):
    def __init__(self, stdout=None, stderr=None):
        self.started = Deferred()
        self.stopped = Deferred()
        self.stdout = stdout
        self.stderr = stderr

    def process_started(self, protocol):
        self.started.callback(protocol)

    def process_stopped(self, protocol, reason):
        self.stopped.callback((protocol, reason))

    def received_stdout(self, protocol, data):
        if self.stdout is not None:
            self.stdout(protocol, data)

    def received_stderr(self, protocol, data):
        if self.stderr is not None:
            self.stderr(protocol, data)


class TestProcessBase(TestCase):
    def _launch_python(self, jobtype, script="i = 42"):
        protocol = ProcessProtocol(jobtype)
        reactor.spawnProcess(
            protocol, "python", ["python", "-c", script])
        return protocol


class TestProtocol(TestProcessBase):
    def test_subclass(self):
        protocol = ProcessProtocol(None)
        self.assertIsInstance(protocol, _ProcessProtocol)

    def test_pid(self):
        fake_jobtype = FakeJobType()
        protocol = self._launch_python(fake_jobtype)
{'fraction_non_alphanumeric': 0.08400435255712731, 'fraction_numerical': 0.00500544069640914, 'mean_word_length': 3.752843846949328, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2221509', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1218, 'n_words': 423}