Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <mailman.11.1724767288.2917.python-list@python.org>
Deutsch   English   Français   Italiano  
<mailman.11.1724767288.2917.python-list@python.org>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: Norman Robins <norman.robins59@gmail.com>
Newsgroups: comp.lang.python
Subject: Mocking a function in a process started with multiprocessing.Process
Date: Tue, 27 Aug 2024 06:48:09 -0700
Lines: 81
Message-ID: <mailman.11.1724767288.2917.python-list@python.org>
References: <CAF2QC-k1KTeM80H+m2k8rzYSgWWzjphYPL=0FpCdEok3joye2w@mail.gmail.com>
 <CAF2QC-=aZ9_U_FV_g4yyLMrbDaye=24nerWeXh7-5Tah-wJ=Qw@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de JvkeQtrmSi+muufmaxIxFQ31i0QkyURDAuWjifoyXyOQ==
Cancel-Lock: sha1:Vru4EcLjiIAF3xvOwCI+sIa7ZEU= sha256:FeafIDwrvyd6lzRp0G5FyXs4UpVDsHiMfssZqCNRdDY=
Return-Path: <norman.robins59@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
 reason="2048-bit key; unprotected key"
 header.d=gmail.com header.i=@gmail.com header.b=E60BtYco;
 dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.012
X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'def': 0.04; 'tests': 0.07;
 'wrong.': 0.07; 'auth': 0.09; 'else:': 0.09; 'example:': 0.09;
 'import': 0.15; 'possible,': 0.15; '->': 0.16; 'dict': 0.16;
 'for.': 0.16; 'received:mail-ej1-x62f.google.com': 0.16;
 'server,': 0.16; 'skip:s 100': 0.16; 'skip:s 150': 0.16;
 'url:test': 0.16; 'server.': 0.19; 'to:addr:python-list': 0.20;
 'tried': 0.26; 'port': 0.26; 'function': 0.27; 'code,': 0.31;
 'gotten': 0.32; 'skip:s 90': 0.32; 'to:name:python': 0.32;
 'message-id:@mail.gmail.com': 0.32; 'but': 0.32; 'server': 0.33;
 'same': 0.34; 'work.': 0.34; 'header:In-Reply-To:1': 0.34;
 'received:google.com': 0.34; 'runs': 0.35; 'from:addr:gmail.com':
 0.35; 'functions': 0.36; 'directory': 0.37; 'file': 0.38; 'valid':
 0.39; 'use': 0.39; 'break': 0.39; 'skip:u 20': 0.39; 'something':
 0.40; 'want': 0.40; 'here': 0.62; 'skip:m 20': 0.63; 'skip:r 20':
 0.64; 'skip:t 20': 0.66; 'skip:t 30': 0.67; 'matter': 0.68;
 'skip:i 40': 0.68; 'url-ip:lookup error': 0.70; 'skip:f 20': 0.75;
 'imports': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1724766501; x=1725371301; darn=python.org;
 h=to:subject:message-id:date:from:in-reply-to:references:mime-version
 :from:to:cc:subject:date:message-id:reply-to;
 bh=SsOMA/wecCcCRKnjnUpuRRuxGRshJ06rSeyWoXIPhKo=;
 b=E60BtYcoadSh8g6sxQUD6eXirkvp1t+3WfaAo9psTRNBcdshKY0XkJNezMv35xpV33
 cq418IZ7BVPM275cdyTmv3INJ4im6sPYUgkZ+XkZGstBdwMtPHrApHTH/7/44KEMJTRK
 /nTT4sVogItJ+D5xUtCwyg+Z6PIqRxAxGf9TDR86mz9UN8CtzTAxNqkc3fWN9WOuxqzB
 4ghF4ayAC7LsnKNcZFHSMubM4z7vSXBODFO2vlO3eS/6FIKbr0IG/mkDtJgM63BOfGgr
 IWqSxyHlpvOblfXhX23NocS+eENka/js6HWaxu1svokkj+n/6qGuGzN/BV2eddfh16t6
 snBw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1724766501; x=1725371301;
 h=to:subject:message-id:date:from:in-reply-to:references:mime-version
 :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
 bh=SsOMA/wecCcCRKnjnUpuRRuxGRshJ06rSeyWoXIPhKo=;
 b=nWtdJiVSz7x/IzbZiHWHoFfqHHlIfUmqHdnIt/fon30Zm9VQ/4itgqCz+F3JEgGIH/
 4IelQ5q+Ab20fGAmDESmfiYNK/AP8eQS8kn+tfqGC2azMY5P4yMMiABHOvqrSnhoy2uU
 bcK1AoH3JoT3sDxlMoOGjdcVdeJ1uR9rR4ToZczUMFO0JtoKSDVG59wpkgkZRkuCK6eo
 YO85Y08HVhqU6nE4b1Ck6GKH5852mx25TvGVwTzVsK5R7fVRfWkq/Z0YkNLrzU838aDf
 KrriwgKeLHzCPpDihQAyPQ6q8/JVsSQ1vVgD00SkjdhudjoFge8JVmnSxP/Em0auIVTz
 lrBA==
X-Gm-Message-State: AOJu0YxsxJhY3LAnWZBZ7HxG2ypcrHys1Du+mkl2YLdQ7eISiwJihGy/
 fmBsYlUgNega7norXJ/8foNH+wImdtK+4H3crt1zngxSpAag3fBFgLFDCT5DqMNXN30kv3jMHJv
 NkGsKP9X6tswOeAzoCyexgOKfJNYczQ==
X-Google-Smtp-Source: AGHT+IEN5+00ynYPF5V1B52e+IaBI7NatJGn/mNIVfUa9hIyDdhObzrzL10ev8iZcL//gVwFPqFhhvGxdYMPLzJassc=
X-Received: by 2002:a17:907:86a1:b0:a86:a30f:4b00 with SMTP id
 a640c23a62f3a-a86e2aa6c45mr274387366b.27.1724766500914; Tue, 27 Aug 2024
 06:48:20 -0700 (PDT)
In-Reply-To: <CAF2QC-k1KTeM80H+m2k8rzYSgWWzjphYPL=0FpCdEok3joye2w@mail.gmail.com>
X-Mailman-Approved-At: Tue, 27 Aug 2024 10:01:28 -0400
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
 <python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
 <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
 <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <CAF2QC-=aZ9_U_FV_g4yyLMrbDaye=24nerWeXh7-5Tah-wJ=Qw@mail.gmail.com>
X-Mailman-Original-References: <CAF2QC-k1KTeM80H+m2k8rzYSgWWzjphYPL=0FpCdEok3joye2w@mail.gmail.com>
Bytes: 7318

Is it possible to mock out a function in a process started with
multiprocessing.Process? AI tells me it is, but I have not gotten it to
work.

The use case is that I have a server I want to write tests for. I want the
tests to start up the server, but I need to mock out the JWT based auth
functions in the server.

If it is possible, perhaps I am doing something wrong.

The function I want to mock out is in foo.bar.baz.validate_jwt`.

The server imports it as from .baz import validate_jwt. I have tried to
mock it many ways, here is the latest attempt:

@pytest.fixture(scope="module")
def mock_get_public_key():
return PUBLIC_KEY

@patch('foo.bar.baz.validate_jwt')
def test_fn(mock_validate_jwt, mock_get_public_key):

I also tried `@patch('foo.bar.main.validate_jwt')`

But no matter what I tried the real functions get called.

Here is an example:

In directory foo/bar file baz.py

def get_public_key(token: str) -> str:
print("in get_public_key)

def validate_jwt(token: str) -> dict
public_key = get_public_key(token)
print("in validate_jwt)

In directory foo/bar file main.py:

from .baz import validate_jwt

def server(token):
valid = validate_jwt(token)

In my test code, which is in test/test_main.py (test and foo are at the
same level)

@pytest.fixture(scope="module")def mock_get_public_key():
    return PUBLIC_KEY
def run_server(port):
    from foo.bar.main import start
    start(is_prod=False, port=port)
@pytest.fixture(scope="module")def mock_validate_jwt(token):
    return {"user": "test_user"}
@pytest.fixture(scope="module")def start_server():
    port = 8002
    server_process = multiprocessing.Process(target=run_server, args=(port,))
    server_process.start()

    max_retries = 10
    for i in range(max_retries):
        try:
            response =
requests.get(f"http://127.0.0.1:{port}/echo?message=test")
            if response.status_code == 200:
                break
        except requests.ConnectionError:
            time.sleep(1)
    else:
        pytest.fail("Server did not start in time")
@pytest.mark.usefixtures("start_server")@patch('dispatcher.main.validate_jwt')def
test_function(mock_validate_jwt, mock_get_public_key):
    ,
    .
    .





When test_function runs the real validate_jwt runs, not my mock.