hyperf / hyperf

🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
https://www.hyperf.io
MIT License
6.15k stars 1.2k forks source link

[BUG] 执行composer test报错:Uncaught Swoole\Error: API must be called in the coroutine #6366

Open luoyou opened 9 months ago

luoyou commented 9 months ago

Execute the command and paste the result below.

Command: uname -a && php -v && composer info | grep hyperf && php --ri swoole

luo@rori:~/code/store$ uname -a && php -v && composer info | grep hyperf && php --ri swoole
Linux rori 5.15.133.1-microsoft-standard-WSL2 #1 SMP Thu Oct 5 21:02:42 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
PHP 8.2.13 (cli) (built: Nov 24 2023 08:46:50) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.13, Copyright (c), by Zend Technologies
friendsofhyperf/pest-plugin-hyperf           v3.1.1       The Pest Hyperf Plugin.
hyperf/cache                                 v3.1.0       A cache component for hyperf.
hyperf/code-parser                           v3.1.0       A code parser component for Hyperf.
hyperf/codec                                 v3.1.0       A codec component for Hyperf.
hyperf/collection                            v3.1.0-rc.13 Hyperf Collection package which come from illuminate/collections
hyperf/command                               v3.1.0-rc.9  Command for hyperf
hyperf/conditionable                         v3.1.0       Hyperf Macroable package which come from illuminate/conditionable
hyperf/config                                v3.1.0       An independent component that provides configuration container.
hyperf/context                               v3.1.0       A coroutine/application context library.
hyperf/contract                              v3.1.0       The contracts of Hyperf.
hyperf/coordinator                           v3.1.0       Hyperf Coordinator
hyperf/coroutine                             v3.1.0       Hyperf Coroutine
hyperf/crontab                               v3.1.0       A crontab component for Hyperf.
hyperf/database                              v3.1.0       A flexible database library.
hyperf/db-connection                         v3.1.0       A hyperf db connection handler for hyperf/database.
hyperf/devtool                               v3.1.0       A Devtool for Hyperf.
hyperf/di                                    v3.1.0       A DI for Hyperf.
hyperf/dispatcher                            v3.1.0       A HTTP Server for Hyperf.
hyperf/engine                                v2.10.3      Coroutine engine provided by swoole.
hyperf/engine-contract                       v1.9.0       Contract for Coroutine Engine
hyperf/event                                 v3.1.0       an event manager that implements PSR-14.
hyperf/exception-handler                     v3.1.0       Exception handler for hyperf
hyperf/filesystem                            v3.1.0       flysystem integration for hyperf
hyperf/flysystem-oss                         v1.2.1
hyperf/framework                             v3.1.0       A coroutine framework that focuses on hyperspeed and flexible, specifically use for build microservices and middlewares.
hyperf/guzzle                                v3.1.0       Swoole coroutine handler for guzzle
hyperf/http-message                          v3.1.0       microservice framework base on swoole
hyperf/http-server                           v3.1.0       A HTTP Server for Hyperf.
hyperf/logger                                v3.1.0       A logger component for hyperf.
hyperf/macroable                             v3.1.0       Hyperf Macroable package which come from illuminate/macroable
hyperf/memory                                v3.1.0       An independent component that use to operate and manage memory.
hyperf/model-cache                           v3.1.0       A model cache component for hyperf.
hyperf/model-listener                        v3.1.0       A model listener for Hyperf.
hyperf/paginator                             v3.1.0       A paginator component for hyperf.
hyperf/pipeline                              v3.1.0       Hyperf Macroable package which come from illuminate/pipeline
hyperf/pool                                  v3.1.0       An independent universal connection pool component.
hyperf/process                               v3.1.0       A process component for hyperf.
hyperf/rate-limit                            v3.1.0       A rate limiter implemented for Hyperf or other coroutine framework
hyperf/redis                                 v3.1.0       A redis component for hyperf.
hyperf/resource                              v3.1.0       A api resource component for hyperf.
hyperf/serializer                            v3.1.0-rc.4  A serializer component for Hyperf.
hyperf/server                                v3.1.0       A base server library for Hyperf.
hyperf/stdlib                                v3.1.0       A stdlib component for Hyperf.
hyperf/stringable                            v3.1.0       Hyperf Stringable package which come from illuminate/support
hyperf/support                               v3.1.0-rc.12 A support component for Hyperf.
hyperf/swagger                               v3.1.0       A swagger library for Hyperf.
hyperf/tappable                              v3.1.0       Hyperf Macroable package which come from illuminate/tappable
hyperf/testing                               v3.1.0       Testing for hyperf
hyperf/token-bucket                          v2.0.0       Implementation of the Token Bucket algorithm.
hyperf/translation                           v3.1.0       An independent translation component, forked by illuminate/translation.
hyperf/utils                                 v3.1.0       A tools package that could help developer solved the problem quickly.
hyperf/validation                            v3.1.0       hyperf validation
hyperf/view                                  v3.1.0       A view library for Hyperf.
hyperf/view-engine                           v3.1.0
hyperf/watcher                               v3.1.0       Hot reload watcher for Hyperf
hyperf/websocket-server                      v3.1.0       A websocket server library for Hyperf.
luoyou/hyperf-json-relation                  1.0.0        基于hyperf的json-relation,解决数据库中使用json字段的关联with问题

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 5.1.0
Built => Nov  6 2023 17:01:06
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1f  31 Mar 2020
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
c-ares => 1.15.0
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
mysqlnd => enabled
async_redis => enabled
coroutine_pgsql => enabled
coroutine_sqlite => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

Description:

看报错信息,应该是通过composer test启动框架,也会启动Crontab定时任务组件,这样的组件内执行redis连接就报这个错误了 image

Steps To Reproduce:

composer test image

limingxinleo commented 8 months ago

你判断下环境,isEnable 如果是单测环境,就直接返回 false 吧

luoyou commented 8 months ago

如何判断启动的环境?

BinaryAlan commented 8 months ago

如何判断启动的环境?

phpunit.xml中定义Env

<php>
    <env name="APP_ENV" value="testing"/>
</php>

就可以用env('APP_ENV') 获取到testing

luoyou commented 8 months ago

如何判断启动的环境?

phpunit.xml中定义Env

<php>
    <env name="APP_ENV" value="testing"/>
</php>

就可以用env('APP_ENV') 获取到testing

测试了三种模式,均无法通过env获取到该环境变量: image image image

deepCyan commented 8 months ago

我记得 2.2 的单测是有些问题的,无法获取到 phpunit.xml 中的 env 配置信息 于是我直接使用 putenv 将单测配置写进了 test/bootstrap.php

BinaryAlan commented 4 months ago

我记得 2.2 的单测是有些问题的,无法获取到 phpunit.xml 中的 env 配置信息 于是我直接使用 putenv 将单测配置写进了 test/bootstrap.php

@limingxinleo 发现项目启动的过程会把phpunit.xml里的env配置信息给覆盖