postgresql 使用嵌入式postgres的测试失败,出现非法状态异常

jum4pzuy  于 2022-12-18  发布在  PostgreSQL
关注(0)|答案(5)|浏览(295)

我正在使用otj-pg-embedded对一个嵌入式postgres数据库运行一些测试。虽然这些测试在本地运行良好,但在Gitlab-CI运行时失败,出现非法状态异常。GitlabCI构建了它,并运行了不包含otj-pg-embedded的测试。
我已经注解掉了测试类的大部分内容,并将问题定位到:

public static SingleInstancePostgresRule pg = EmbeddedPostgresRules.singleInstance();
import com.goldfinger.models.AuditLog;
import com.opentable.db.postgres.embedded.FlywayPreparer;
import com.opentable.db.postgres.junit.EmbeddedPostgresRules;
import com.opentable.db.postgres.junit.PreparedDbRule;
import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
public class SQLAuditRepositoryTest {

    private static SQLAuditRepository sqlAuditRepository;
    private static AuditLog auditLog_1;
    private static AuditLog auditLog_2;
    private static AuditLog auditLog_3;
    private static List<AuditLog> auditLogList;

    @ClassRule
        public static SingleInstancePostgresRule pg = EmbeddedPostgresRules.singleInstance();

    @Test
    public void simpleTest() {
        assert (2 == 2);
    }
}

这是堆栈跟踪:

java.lang.IllegalStateException: Process [/tmp/embedded-pg/PG-06e3a92a2edb6ddd6dbdf5602d0252ca/bin/initdb, -A, trust, -U, postgres, -D, /tmp/epg6584640257265165384, -E, UTF-8] failed

    at com.opentable.db.postgres.embedded.EmbeddedPostgres.system(EmbeddedPostgres.java:626)
    at com.opentable.db.postgres.embedded.EmbeddedPostgres.initdb(EmbeddedPostgres.java:240)
...
... many lines here
...
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:745)


这是一个gitlab-ci.yml

image: java:latest
services:
  - postgres:latest
before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle

package:
  stage: build
  script:
    - ./gradlew assemble

test:
  stage: test
  script:
  - ./gradlew check
  artifacts:
    reports:
      junit: build/test-results/test/*.xml

任何帮助都将不胜感激。

h9vpoimq

h9vpoimq1#

如果升级到macOS 11.x+(升级到BigSur)后停止

对于Mac用户(特别是新的BigSur),已经有一个issue可以解决这个问题。
目前还没有一个明确的“修复”,但我已经通过安装postgresql的工作:

brew install postgresql

不确定是否同样适用于其他操作系统。

mzmfm0qo

mzmfm0qo2#

我在MacOS M1上运行嵌入式postgres测试时遇到过类似的问题:

java.lang.IllegalStateException: Process [/var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/embedded-pg/PG-4f38c8a8b2500189835f8ec1b75de81b/bin/initdb, -A, trust, -U, postgres, -D, /var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/pg-embedded-009358be-3e95-4dbe-aa7a-01a9008511cb-3277843270072134584/epg524445573999525527, -E, UTF-8] failed

我复制粘贴命令并在shell上运行它:

/var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/embedded-pg/PG-4f38c8a8b2500189835f8ec1b75de81b/bin/initdb -A trust -U postgres -D /var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/pg-embedded-009358be-3e95-4dbe-aa7a-01a9008511cb-3277843270072134584/epg524445573999525527 -E UTF-8

输出显示了一个提示:

Running bootstrap script ... 2021-02-17 08:18:21.008 WET [28692] FATAL:  could not create shared memory segment: Cannot allocate memory
2021-02-17 08:18:21.008 WET [28692] DETAIL:  Failed system call was shmget(key=5432001, size=56, 03600).
2021-02-17 08:18:21.008 WET [28692] HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMALL parameter.  You might need to reconfigure the kernel with larger SHMALL.
    The PostgreSQL documentation contains more information about shared memory configuration.
child process exited with exit code 1
initdb: removing contents of data directory "/var/folders/g_/jxzv9glj0p13njlzpr9xtcfm0000gn/T/pg-embedded-cbe6f5f8-24b2-4431-afec-6e7aeb1dbb5d-8656684715588054403/epg6904898061245880853"


然后我在贝壳上运行:

sudo sysctl kern.sysv.shmmax=104857600
sudo sysctl kern.sysv.shmall=25600

而且奏效了。

ccrfmcuu

ccrfmcuu3#

在macOS 12.1 Monterey中,我通过下载postgres二进制文件并让EmbeddedPostgres来解决这些问题,如我在这里所述

# download binaries, here for postgres 12.9
curl -O http://get.enterprisedb.com/postgresql/postgresql-12.9-1-osx-binaries.zip --output-dir /tmp/

# unzip it in /tmp/
unzip /tmp/postgresql-12.9-1-osx-binaries.zip -d /tmp/postgresql-12.9-1-osx-binaries/

然后在代码中:

import com.opentable.db.postgres.embedded.EmbeddedPostgres;
import java.io.File;

EmbeddedPostgres pg = EmbeddedPostgres
  .builder()
  .setPgDirectoryResolver(
     dir -> new File("/tmp/postgresql-12.9-1-osx-binaries/pgsql/"))
  .start();
q8l4jmvw

q8l4jmvw4#

我在flyway/embedded-pg上遇到过同样的问题。看起来临时文件在某个时候损坏了。重新启动会修复它(直到它再次发生)或者你可以删除/tmp/embedded-pg/目录中的PG-*

zphenhs4

zphenhs45#

Windows用户可能会遇到类似的问题。我的情况是当尝试运行嵌入式postgres的集成测试时:
java.lang.IllegalStateException:进程[C:\用户...\应用程序数据\本地\临时\嵌入式页面\PG-02224 a4 f280160581 d2144 dec 70 deaf 8\bin\initdb.exe,-A,信任,-U,postgres,-D,C:\用户...\应用程序数据\本地\临时\epg 7146074401943849308,-E,UTF-8]失败
当尝试从终端运行此命令时,您将遇到类似以下的dll库问题:
加载共享库时出错:msvcr120.dll:无法打开共享目标文件:没有这样的文件或目录
要解决此问题,应安装Visual C++可再发行包for Visual Studio 2013。您可以从官方microsoft site中找到它

相关问题