LCOV - differential code coverage report
Current view: top level - src/interfaces/ecpg/test/thread - thread.pgc (source / functions) Coverage Total Hit UBC CBC
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 85.0 % 40 34 6 34
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 2 2 2
Baseline: 16@8cea358b128 Branches: 62.5 % 24 15 9 15
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (240..) days: 85.0 % 40 34 6 34
Function coverage date bins:
(240..) days: 100.0 % 2 2 2
Branch coverage date bins:
(240..) days: 62.5 % 24 15 9 15

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*
                                  2                 :                :  *  Thread test program
                                  3                 :                :  *  by Philip Yarra & Lee Kindness.
                                  4                 :                :  */
                                  5                 :                : #include <stdint.h>
                                  6                 :                : #include <stdlib.h>
                                  7                 :                : #include "ecpg_config.h"
                                  8                 :                : 
                                  9                 :                : #ifndef WIN32
                                 10                 :                : #include <pthread.h>
                                 11                 :                : #else
                                 12                 :                : #include <windows.h>
                                 13                 :                : #include <locale.h>
                                 14                 :                : #endif
                                 15                 :                : 
                                 16                 :                : exec sql include ../regression;
                                 17                 :                : 
                                 18                 :                : void *test_thread(void *arg);
                                 19                 :                : 
                                 20                 :                : int nthreads   = 10;
                                 21                 :                : int iterations = 20;
                                 22                 :                : 
 5443 meskes@postgresql.or       23                 :CBC           1 : int main()
                                 24                 :                : {
                                 25                 :                : #ifndef WIN32
                                 26                 :                :   pthread_t *threads;
                                 27                 :                : #else
                                 28                 :                :   HANDLE *threads;
                                 29                 :                : #endif
                                 30                 :                :   intptr_t n;
                                 31                 :                :   EXEC SQL BEGIN DECLARE SECTION;
                                 32                 :                :   int l_rows;
                                 33                 :                :   EXEC SQL END DECLARE SECTION;
                                 34                 :                : 
                                 35                 :                :  /* Do not switch on debug output for regression tests. The threads get executed in
                                 36                 :                :   * more or less random order */
                                 37                 :                :  /* ECPGdebug(1, stderr); */
                                 38                 :                : 
                                 39                 :                :   /* setup test_thread table */
 6465                            40                 :              1 :   EXEC SQL CONNECT TO REGRESSDB1;
                                 41                 :              1 :   EXEC SQL DROP TABLE test_thread; /* DROP might fail */
                                 42                 :              1 :   EXEC SQL COMMIT;
                                 43                 :              1 :   EXEC SQL CREATE TABLE
                                 44                 :                :     test_thread(tstamp    TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
                                 45                 :                :         thread    TEXT      NOT NULL,
                                 46                 :                :         iteration INTEGER   NOT NULL,
                                 47                 :                :         PRIMARY KEY(thread, iteration));
                                 48                 :              1 :   EXEC SQL COMMIT;
                                 49                 :              1 :   EXEC SQL DISCONNECT;
                                 50                 :                : 
                                 51                 :                :   /* create, and start, threads */
 6226 magnus@hagander.net        52                 :              1 :   threads = calloc(nthreads, sizeof(threads[0]));
 6465 meskes@postgresql.or       53         [ -  + ]:              1 :   if( threads == NULL )
                                 54                 :                :     {
 6465 meskes@postgresql.or       55                 :UBC           0 :       fprintf(stderr, "Cannot alloc memory\n");
 2432 peter_e@gmx.net            56                 :              0 :       return 1;
                                 57                 :                :     }
 6465 meskes@postgresql.or       58         [ +  + ]:CBC          11 :   for( n = 0; n < nthreads; n++ )
                                 59                 :                :     {
                                 60                 :                : #ifndef WIN32
 1514 peter@eisentraut.org       61                 :             10 :       pthread_create(&threads[n], NULL, test_thread, (void *) (n + 1));
                                 62                 :                : #else
                                 63                 :                :       threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) (void (*) (void)) test_thread, (void *) (n + 1), 0, NULL);
                                 64                 :                : #endif
                                 65                 :                :     }
                                 66                 :                : 
                                 67                 :                :   /* wait for thread completion */
                                 68                 :                : #ifndef WIN32
 6465 meskes@postgresql.or       69         [ +  + ]:             11 :   for( n = 0; n < nthreads; n++ )
                                 70                 :                :     {
                                 71                 :             10 :       pthread_join(threads[n], NULL);
                                 72                 :                :     }
                                 73                 :                : #else
                                 74                 :                :   WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
                                 75                 :                : #endif
                                 76                 :              1 :   free(threads);
                                 77                 :                : 
                                 78                 :                :   /* and check results */
                                 79                 :              1 :   EXEC SQL CONNECT TO REGRESSDB1;
                                 80                 :              1 :   EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
                                 81                 :              1 :   EXEC SQL COMMIT;
                                 82                 :              1 :   EXEC SQL DISCONNECT;
                                 83         [ +  - ]:              1 :   if( l_rows == (nthreads * iterations) )
                                 84                 :              1 :     printf("Success.\n");
                                 85                 :                :   else
 6465 meskes@postgresql.or       86                 :UBC           0 :     printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
                                 87                 :                : 
 2432 peter_e@gmx.net            88                 :CBC           1 :   return 0;
                                 89                 :                : }
                                 90                 :                : 
 6465 meskes@postgresql.or       91                 :             10 : void *test_thread(void *arg)
                                 92                 :                : {
 1514 peter@eisentraut.org       93                 :             10 :   long threadnum = (intptr_t) arg;
                                 94                 :                : 
                                 95                 :                :   EXEC SQL BEGIN DECLARE SECTION;
                                 96                 :                :   int  l_i;
                                 97                 :                :   char l_connection[128];
                                 98                 :                :   EXEC SQL END DECLARE SECTION;
                                 99                 :                : 
                                100                 :                :   /* build up connection name, and connect to database */
                                101                 :                : #ifndef _MSC_VER
 6465 meskes@postgresql.or      102                 :             10 :   snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
                                103                 :                : #else
                                104                 :                :   _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
                                105                 :                : #endif
                                106                 :                :   EXEC SQL WHENEVER sqlerror sqlprint;
                                107                 :             10 :   EXEC SQL CONNECT TO REGRESSDB1 AS :l_connection;
                                108   [ -  +  -  + ]:             10 :   if( sqlca.sqlcode != 0 )
                                109                 :                :     {
 6465 meskes@postgresql.or      110                 :UBC           0 :       printf("%s: ERROR: cannot connect to database!\n", l_connection);
 2432 peter_e@gmx.net           111                 :              0 :       return NULL;
                                112                 :                :     }
 6465 meskes@postgresql.or      113                 :CBC          10 :   EXEC SQL AT :l_connection BEGIN;
                                114         [ -  + ]:             10 : 
                                115                 :                :   /* insert into test_thread table */
                                116         [ +  + ]:            210 :   for( l_i = 1; l_i <= iterations; l_i++ )
                                117                 :                :     {
                                118                 :            200 :       EXEC SQL AT :l_connection INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
 6226                           119   [ -  +  -  + ]:            200 :       if( sqlca.sqlcode != 0 )
 6465 meskes@postgresql.or      120                 :UBC           0 :     printf("%s: ERROR: insert failed!\n", l_connection);
                                121                 :                :     }
                                122                 :                : 
                                123                 :                :   /* all done */
 6465 meskes@postgresql.or      124                 :CBC          10 :   EXEC SQL AT :l_connection COMMIT;
                                125         [ -  + ]:             10 :   EXEC SQL DISCONNECT :l_connection;
 2432 peter_e@gmx.net           126         [ -  + ]:             10 :   return NULL;
                                127                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622