github.com/tuhaihe/gpbackup@v1.0.3/integration/utils_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"time"
     9  
    10  	"github.com/tuhaihe/gp-common-go-libs/dbconn"
    11  	"github.com/tuhaihe/gp-common-go-libs/testhelper"
    12  	fp "github.com/tuhaihe/gpbackup/filepath"
    13  	"github.com/tuhaihe/gpbackup/testutils"
    14  	"github.com/tuhaihe/gpbackup/utils"
    15  
    16  	"golang.org/x/sys/unix"
    17  
    18  	. "github.com/onsi/ginkgo/v2"
    19  	. "github.com/onsi/gomega"
    20  )
    21  
    22  var _ = Describe("utils integration", func() {
    23  	It("TerminateHangingCopySessions stops hanging COPY sessions", func() {
    24  		tempDir, err := ioutil.TempDir("", "temp")
    25  		Expect(err).To(Not(HaveOccurred()))
    26  		defer os.Remove(tempDir)
    27  		testPipe := filepath.Join(tempDir, "test_pipe")
    28  		conn := testutils.SetupTestDbConn("testdb")
    29  		defer conn.Close()
    30  
    31  		fpInfo := fp.FilePathInfo{
    32  			PID:       1,
    33  			Timestamp: "11223344556677",
    34  		}
    35  
    36  		testhelper.AssertQueryRuns(conn, "SET application_name TO 'hangingApplication'")
    37  		testhelper.AssertQueryRuns(conn, "CREATE TABLE public.foo(i int)")
    38  		// TODO: this works without error in 6, but throws an error in 7.  Still functions, though.  Unclear why the change.
    39  		// defer testhelper.AssertQueryRuns(conn, "DROP TABLE public.foo")
    40  		defer connectionPool.MustExec("DROP TABLE public.foo")
    41  		err = unix.Mkfifo(testPipe, 0777)
    42  		Expect(err).To(Not(HaveOccurred()))
    43  		defer os.Remove(testPipe)
    44  		go func() {
    45  			copyFileName := fpInfo.GetSegmentPipePathForCopyCommand()
    46  			// COPY will blcok because there is no reader for the testPipe
    47  			_, _ = conn.Exec(fmt.Sprintf("COPY public.foo TO PROGRAM 'echo %s > /dev/null; cat - > %s' WITH CSV DELIMITER ','", copyFileName, testPipe))
    48  		}()
    49  
    50  		query := `SELECT count(*) FROM pg_stat_activity WHERE application_name = 'hangingApplication'`
    51  		Eventually(func() string { return dbconn.MustSelectString(connectionPool, query) }, 5*time.Second, 100*time.Millisecond).Should(Equal("1"))
    52  
    53  		utils.TerminateHangingCopySessions(connectionPool, fpInfo, "hangingApplication")
    54  
    55  		Eventually(func() string { return dbconn.MustSelectString(connectionPool, query) }, 5*time.Second, 100*time.Millisecond).Should(Equal("0"))
    56  
    57  	})
    58  })