func TestStressConcurrency(t *testing.T) {
	metrics := []telegraf.Metric{
		newMetric(t, "", MSS{"foo": "bar"}, MSI{"a": 1}),
		newMetric(t, "", MSS{"pop": "tart"}, MSI{"b": 1}),
		newMetric(t, "", MSS{"foo": "bar", "pop": "tart"}, MSI{"a": 2, "b": 2}),
		newMetric(t, "_b", MSS{"foo": "bar"}, MSI{"a": 1}),
	}

	concurrency := 4
	loops := 100

	pctl := newPostgresqlTest(t)
	pctl.Logger.emitLevel = pgx.LogLevelWarn
	require.NoError(t, pctl.Connect())

	for i := 0; i < loops; i++ {
		var wgStart, wgDone sync.WaitGroup
		wgStart.Add(concurrency)
		wgDone.Add(concurrency)
		for j := 0; j < concurrency; j++ {
			go func() {
				mShuf := make([]telegraf.Metric, len(metrics))
				copy(mShuf, metrics)
				rand.Shuffle(len(mShuf), func(a, b int) { mShuf[a], mShuf[b] = mShuf[b], mShuf[a] })

				p := newPostgresqlTest(t)
				p.TagsAsForeignKeys = true
				p.Logger.emitLevel = pgx.LogLevelWarn
				p.dbConfig.MaxConns = int32(rand.Intn(3) + 1)
				require.NoError(t, p.Connect())
				wgStart.Done()
				wgStart.Wait()

				err := p.Write(mShuf)
				assert.NoError(t, err)
				assert.NoError(t, p.Close())
				assert.False(t, p.Logger.HasLevel(pgx.LogLevelWarn))
				wgDone.Done()
			}()
		}
		wgDone.Wait()

		if t.Failed() {
			break
		}

		for _, stmt := range []string{
			"DROP TABLE \"" + t.Name() + "_tag\"",
			"DROP TABLE \"" + t.Name() + "\"",
			"DROP TABLE \"" + t.Name() + "_b_tag\"",
			"DROP TABLE \"" + t.Name() + "_b\"",
		} {
			_, err := pctl.db.Exec(ctx, stmt)
			require.NoError(t, err)
		}
	}
}
// Last ditch effort to find any concurrency issues.
Last ditch effort to find any concurrency issues.
// Last ditch effort to find any concurrency issues .
func TestStressConcurrency(t *testing.T) {
	metrics := []telegraf.Metric{
		newMetric(t, "", MSS{"foo": "bar"}, MSI{"a": 1}),
		newMetric(t, "", MSS{"pop": "tart"}, MSI{"b": 1}),
		newMetric(t, "", MSS{"foo": "bar", "pop": "tart"}, MSI{"a": 2, "b": 2}),
		newMetric(t, "_b", MSS{"foo": "bar"}, MSI{"a": 1}),
	}
	concurrency := 4
	loops := 100
	pctl := newPostgresqlTest(t)
	pctl.Logger.emitLevel = pgx.LogLevelWarn
	require.NoError(t, pctl.Connect())
	for i := 0; i < loops; i++ {
		var wgStart, wgDone sync.WaitGroup
		wgStart.Add(concurrency)
		wgDone.Add(concurrency)
		for j := 0; j < concurrency; j++ {
			go func() {
				mShuf := make([]telegraf.Metric, len(metrics))
				copy(mShuf, metrics)
				rand.Shuffle(len(mShuf), func(a, b int) { mShuf[a], mShuf[b] = mShuf[b], mShuf[a] })
				p := newPostgresqlTest(t)
				p.TagsAsForeignKeys = true
				p.Logger.emitLevel = pgx.LogLevelWarn
				p.dbConfig.MaxConns = int32(rand.Intn(3) + 1)
				require.NoError(t, p.Connect())
				wgStart.Done()
				wgStart.Wait()
				err := p.Write(mShuf)
				assert.NoError(t, err)
				assert.NoError(t, p.Close())
				assert.False(t, p.Logger.HasLevel(pgx.LogLevelWarn))
				wgDone.Done()
			}()
		}
		wgDone.Wait()
		if t.Failed() {
			break
		}
		for _, stmt := range []string{
			"DROP TABLE \"" + t.Name() + "_tag\"",
			"DROP TABLE \"" + t.Name() + "\"",
			"DROP TABLE \"" + t.Name() + "_b_tag\"",
			"DROP TABLE \"" + t.Name() + "_b\"",
		} {
			_, err := pctl.db.Exec(ctx, stmt)
			require.NoError(t, err)
		}
	}
}
func TestStressConcurrency ( t * testing . T ) { metrics := [ ] telegraf . Metric { newMetric ( t , " " , MSS { " " : " " } , MSI { " " : 1 } ) , newMetric ( t , " " , MSS { " " : " " } , MSI { " " : 1 } ) , newMetric ( t , " " , MSS { " " : " " , " " : " " } , MSI { " " : 2 , " " : 2 } ) , newMetric ( t , " " , MSS { " " : " " } , MSI { " " : 1 } ) , } concurrency := 4 loops := 100 pctl := newPostgresqlTest ( t ) pctl . Logger . emitLevel = pgx . LogLevelWarn require . NoError ( t , pctl . Connect ( ) ) for i := 0 ; i < loops ; i ++ { var wgStart , wgDone sync . WaitGroup wgStart . Add ( concurrency ) wgDone . Add ( concurrency ) for j := 0 ; j < concurrency ; j ++ { go func ( ) { mShuf := make ( [ ] telegraf . Metric , len ( metrics ) ) copy ( mShuf , metrics ) rand . Shuffle ( len ( mShuf ) , func ( a , b int ) { mShuf [ a ] , mShuf [ b ] = mShuf [ b ] , mShuf [ a ] } ) p := newPostgresqlTest ( t ) p . TagsAsForeignKeys = true p . Logger . emitLevel = pgx . LogLevelWarn p . dbConfig . MaxConns = int32 ( rand . Intn ( 3 ) + 1 ) require . NoError ( t , p . Connect ( ) ) wgStart . Done ( ) wgStart . Wait ( ) err := p . Write ( mShuf ) assert . NoError ( t , err ) assert . NoError ( t , p . Close ( ) ) assert . False ( t , p . Logger . HasLevel ( pgx . LogLevelWarn ) ) wgDone . Done ( ) } ( ) } wgDone . Wait ( ) if t . Failed ( ) { break } for _ , stmt := range [ ] string { " \" " + t . Name ( ) + " \" " , " \" " + t . Name ( ) + " \" " , " \" " + t . Name ( ) + " \" " , " \" " + t . Name ( ) + " \" " , } { _ , err := pctl . db . Exec ( ctx , stmt ) require . NoError ( t , err ) } } }
Last ditch effort to find any concurrency issues.
Last ditch effort to find any concurrency issues .