From 987866a0f57170c245fcb8aa7c92b1db953c414d Mon Sep 17 00:00:00 2001
From: "ldming" <ldming101@gmail.com>
Date: Mon, 24 Jan 2022 17:30:54 +0800
Subject: [PATCH] add dsa test module

---
 src/test/modules/test_dsa/.gitignore          |  4 +
 src/test/modules/test_dsa/Makefile            | 23 ++++++
 src/test/modules/test_dsa/README              |  1 +
 .../modules/test_dsa/expected/test_dsa.out    |  7 ++
 src/test/modules/test_dsa/sql/test_dsa.sql    |  3 +
 src/test/modules/test_dsa/test_dsa--1.0.sql   |  8 ++
 src/test/modules/test_dsa/test_dsa.c          | 80 +++++++++++++++++++
 src/test/modules/test_dsa/test_dsa.control    |  4 +
 8 files changed, 130 insertions(+)
 create mode 100644 src/test/modules/test_dsa/.gitignore
 create mode 100644 src/test/modules/test_dsa/Makefile
 create mode 100644 src/test/modules/test_dsa/README
 create mode 100644 src/test/modules/test_dsa/expected/test_dsa.out
 create mode 100644 src/test/modules/test_dsa/sql/test_dsa.sql
 create mode 100644 src/test/modules/test_dsa/test_dsa--1.0.sql
 create mode 100644 src/test/modules/test_dsa/test_dsa.c
 create mode 100644 src/test/modules/test_dsa/test_dsa.control

diff --git a/src/test/modules/test_dsa/.gitignore b/src/test/modules/test_dsa/.gitignore
new file mode 100644
index 0000000000..5dcb3ff972
--- /dev/null
+++ b/src/test/modules/test_dsa/.gitignore
@@ -0,0 +1,4 @@
+# Generated subdirectories
+/log/
+/results/
+/tmp_check/
diff --git a/src/test/modules/test_dsa/Makefile b/src/test/modules/test_dsa/Makefile
new file mode 100644
index 0000000000..52c90cf8cb
--- /dev/null
+++ b/src/test/modules/test_dsa/Makefile
@@ -0,0 +1,23 @@
+# src/test/modules/test_dsa/Makefile
+
+MODULE_big = test_dsa
+OBJS = \
+	$(WIN32RES) \
+	test_dsa.o
+PGFILEDESC = "test_dsa - test code for dsa"
+
+EXTENSION = test_dsa
+DATA = test_dsa--1.0.sql
+
+REGRESS = test_dsa
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = src/test/modules/test_dsa
+top_builddir = ../../../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/src/test/modules/test_dsa/README b/src/test/modules/test_dsa/README
new file mode 100644
index 0000000000..737fad4ee9
--- /dev/null
+++ b/src/test/modules/test_dsa/README
@@ -0,0 +1 @@
+test_dsa is a test module for dynamic shared memory.
\ No newline at end of file
diff --git a/src/test/modules/test_dsa/expected/test_dsa.out b/src/test/modules/test_dsa/expected/test_dsa.out
new file mode 100644
index 0000000000..d284534f01
--- /dev/null
+++ b/src/test/modules/test_dsa/expected/test_dsa.out
@@ -0,0 +1,7 @@
+CREATE EXTENSION test_dsa;
+SELECT test_dsa();
+ test_dsa 
+----------
+ 
+(1 row)
+
diff --git a/src/test/modules/test_dsa/sql/test_dsa.sql b/src/test/modules/test_dsa/sql/test_dsa.sql
new file mode 100644
index 0000000000..dcb05bf795
--- /dev/null
+++ b/src/test/modules/test_dsa/sql/test_dsa.sql
@@ -0,0 +1,3 @@
+CREATE EXTENSION test_dsa;
+
+SELECT test_dsa();
diff --git a/src/test/modules/test_dsa/test_dsa--1.0.sql b/src/test/modules/test_dsa/test_dsa--1.0.sql
new file mode 100644
index 0000000000..2357cfc3da
--- /dev/null
+++ b/src/test/modules/test_dsa/test_dsa--1.0.sql
@@ -0,0 +1,8 @@
+/* src/test/modules/test_dsa/test_dsa--1.0.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION test_dsa" to load this file. \quit
+
+CREATE FUNCTION test_dsa()
+	RETURNS pg_catalog.void STRICT
+	AS 'MODULE_PATHNAME' LANGUAGE C;
diff --git a/src/test/modules/test_dsa/test_dsa.c b/src/test/modules/test_dsa/test_dsa.c
new file mode 100644
index 0000000000..d5f4a37bd2
--- /dev/null
+++ b/src/test/modules/test_dsa/test_dsa.c
@@ -0,0 +1,80 @@
+/*--------------------------------------------------------------------------
+ *
+ * test_dsa.c
+ *		Test for dsa.
+ *
+ * Copyright (c) 2009-2022, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *		src/test/modules/test_dsa/test_dsa.c
+ *
+ * -------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "fmgr.h"
+#include "utils/dsa.h"
+
+PG_MODULE_MAGIC;
+
+/* 
+ * 1. alloate memory until failure
+ * 2. free all allocated memory
+ */
+static void
+test_dsa_alloc_free(dsa_area *area)
+{
+	int	i;
+	int num = 0;
+	dsa_pointer p[256] = {InvalidDsaPointer};
+
+	dsa_dump(area);
+	elog(LOG, "allocating memory ...");
+	for (i = 0; i < 256; i++)
+	{
+		dsa_pointer pointer = dsa_allocate_extended(area, 4096, DSA_ALLOC_NO_OOM);
+		if (DsaPointerIsValid(pointer))
+		{
+			num++;
+			p[i] = pointer;
+		}
+		else
+		{
+			elog(LOG, "allocate failed.");
+			break;
+		}
+	}
+
+	elog(LOG, "allocated %d", num);
+	dsa_dump(area);
+	elog(LOG, "free all allocated memory");
+	for (i = 0; i < num; i++)
+	{
+		Assert(DsaPointerIsValid(p[i]));
+		dsa_free(area, p[i]);
+	}
+	dsa_dump(area);
+}
+
+/*
+ * SQL-callable entry point to perform all tests
+ *
+ * Argument is the number of entries to put in the trees
+ */
+PG_FUNCTION_INFO_V1(test_dsa);
+
+Datum
+test_dsa(PG_FUNCTION_ARGS)
+{
+#define MOCK_TRANCHE_ID	100
+
+	dsa_area *area = dsa_create(MOCK_TRANCHE_ID);
+	dsa_set_size_limit(area, 1*1024*1024);
+
+	test_dsa_alloc_free(area);
+	test_dsa_alloc_free(area);
+
+	dsa_detach(area);
+	PG_RETURN_VOID();
+}
diff --git a/src/test/modules/test_dsa/test_dsa.control b/src/test/modules/test_dsa/test_dsa.control
new file mode 100644
index 0000000000..c38f781aa4
--- /dev/null
+++ b/src/test/modules/test_dsa/test_dsa.control
@@ -0,0 +1,4 @@
+comment = 'Test code for dsa'
+default_version = '1.0'
+module_pathname = '$libdir/test_dsa'
+relocatable = true
-- 
2.32.0.3.g01195cf9f

