github.com/Tri-stone/burrow@v0.25.0/vent/sqldb/sqldb_postgres_test.go (about) 1 // +build integration 2 3 package sqldb_test 4 5 import ( 6 "encoding/json" 7 "fmt" 8 "strconv" 9 "testing" 10 "time" 11 12 "github.com/lib/pq" 13 14 "github.com/hyperledger/burrow/vent/sqldb/adapters" 15 "github.com/hyperledger/burrow/vent/types" 16 17 "github.com/hyperledger/burrow/vent/test" 18 "github.com/stretchr/testify/require" 19 ) 20 21 func TestPostgresSynchronizeDB(t *testing.T) { 22 testSynchronizeDB(t, test.PostgresVentConfig("")) 23 } 24 25 func TestPostgresCleanDB(t *testing.T) { 26 testCleanDB(t, test.PostgresVentConfig("")) 27 } 28 29 func TestPostgresSetBlock(t *testing.T) { 30 testSetBlock(t, test.PostgresVentConfig("")) 31 } 32 33 func TestPostgresBlockNotification(t *testing.T) { 34 cfg := test.PostgresVentConfig("") 35 db, closeDB := test.NewTestDB(t, cfg) 36 defer closeDB() 37 38 errp := db.Ping() 39 require.NoError(t, errp) 40 41 functionName := "notify_height" 42 channelName := "height_notification" 43 pad := db.DBAdapter.(*adapters.PostgresAdapter) 44 45 for i := 0; i < 2; i++ { 46 query := pad.CreateNotifyFunctionQuery(functionName, channelName, types.SQLColumnLabelHeight) 47 _, err := db.DB.Exec(query) 48 require.NoError(t, err) 49 50 query = pad.CreateTriggerQuery("notify_height_trigger", types.SQLLogTableName, functionName) 51 _, err = db.DB.Exec(query) 52 require.NoError(t, err) 53 } 54 55 listener := pq.NewListener(cfg.DBURL, time.Second, time.Second*20, func(event pq.ListenerEventType, err error) { 56 require.NoError(t, err) 57 }) 58 err := listener.Listen(channelName) 59 require.NoError(t, err) 60 61 // new block 62 str, dat := getBlock() 63 64 errCh := make(chan error) 65 go func() { 66 type payload struct { 67 Height string `json:"_height"` 68 } 69 for n := range listener.NotificationChannel() { 70 pl := new(payload) 71 err := json.Unmarshal([]byte(n.Extra), pl) 72 if err != nil { 73 errCh <- err 74 return 75 } 76 if pl.Height != "" { 77 if strconv.FormatUint(dat.BlockHeight, 10) != pl.Height { 78 errCh <- fmt.Errorf("got height %s from notification but expected %d", 79 pl.Height, dat.BlockHeight) 80 } 81 errCh <- nil 82 return 83 } 84 } 85 }() 86 87 // Set it 88 err = db.SetBlock(str, dat) 89 require.NoError(t, err) 90 91 // read 92 _, err = db.GetLastBlockHeight() 93 require.NoError(t, err) 94 95 _, err = db.GetBlock(dat.BlockHeight) 96 require.NoError(t, err) 97 98 require.NoError(t, <-errCh) 99 }