55 "context"
66 "errors"
77 "fmt"
8+ "strings"
89 "sync"
910 "testing"
1011 "time"
@@ -36,8 +37,14 @@ var emptyOptions = []byte{}
3637// TestProxy runs the go-da DA test suite against the JSONRPC service
3738// NOTE: This test requires a test JSONRPC service to run on the port
3839// 3450 which is chosen to be sufficiently distinct from the default port
40+
41+ func getTestDABlockTime () time.Duration {
42+ return 100 * time .Millisecond
43+ }
44+
3945func TestProxy (t * testing.T ) {
40- dummy := coreda .NewDummyDA (100_000 , 0 , 0 , 10 * time .Second )
46+ dummy := coreda .NewDummyDA (100_000 , 0 , 0 , getTestDABlockTime ())
47+ dummy .StartHeightTicker ()
4148 logger := log .NewTestLogger (t )
4249 server := proxy .NewServer (logger , ServerHost , ServerPort , dummy )
4350 err := server .Start (context .Background ())
@@ -50,25 +57,21 @@ func TestProxy(t *testing.T) {
5057
5158 client , err := proxy .NewClient (context .Background (), logger , ClientURL , "" , "74657374" )
5259 require .NoError (t , err )
53- RunDATestSuite (t , & client .DA )
54- }
5560
56- // RunDATestSuite runs all tests against given DA
57- func RunDATestSuite (t * testing.T , d coreda.DA ) {
5861 t .Run ("Basic DA test" , func (t * testing.T ) {
59- BasicDATest (t , d )
62+ BasicDATest (t , & client . DA )
6063 })
6164 t .Run ("Get IDs and all data" , func (t * testing.T ) {
62- GetIDsTest (t , d )
65+ GetIDsTest (t , & client . DA )
6366 })
6467 t .Run ("Check Errors" , func (t * testing.T ) {
65- CheckErrors (t , d )
68+ CheckErrors (t , & client . DA )
6669 })
6770 t .Run ("Concurrent read/write test" , func (t * testing.T ) {
68- ConcurrentReadWriteTest (t , d )
71+ ConcurrentReadWriteTest (t , & client . DA )
6972 })
7073 t .Run ("Given height is from the future" , func (t * testing.T ) {
71- HeightFromFutureTest (t , d )
74+ HeightFromFutureTest (t , & client . DA )
7275 })
7376}
7477
@@ -77,7 +80,7 @@ func BasicDATest(t *testing.T, d coreda.DA) {
7780 msg1 := []byte ("message 1" )
7881 msg2 := []byte ("message 2" )
7982
80- ctx := context . TODO ()
83+ ctx := t . Context ()
8184 id1 , err := d .Submit (ctx , []coreda.Blob {msg1 }, 0 , testNamespace )
8285 assert .NoError (t , err )
8386 assert .NotEmpty (t , id1 )
@@ -86,6 +89,8 @@ func BasicDATest(t *testing.T, d coreda.DA) {
8689 assert .NoError (t , err )
8790 assert .NotEmpty (t , id2 )
8891
92+ time .Sleep (getTestDABlockTime ())
93+
8994 id3 , err := d .SubmitWithOptions (ctx , []coreda.Blob {msg1 }, 0 , testNamespace , []byte ("random options" ))
9095 assert .NoError (t , err )
9196 assert .NotEmpty (t , id3 )
@@ -119,7 +124,7 @@ func BasicDATest(t *testing.T, d coreda.DA) {
119124
120125// CheckErrors ensures that errors are handled properly by DA.
121126func CheckErrors (t * testing.T , d coreda.DA ) {
122- ctx := context . TODO ()
127+ ctx := t . Context ()
123128 blob , err := d .Get (ctx , []coreda.ID {[]byte ("invalid blob id" )}, testNamespace )
124129 assert .Error (t , err )
125130 assert .ErrorContains (t , err , coreda .ErrBlobNotFound .Error ())
@@ -130,7 +135,7 @@ func CheckErrors(t *testing.T, d coreda.DA) {
130135func GetIDsTest (t * testing.T , d coreda.DA ) {
131136 msgs := []coreda.Blob {[]byte ("msg1" ), []byte ("msg2" ), []byte ("msg3" )}
132137
133- ctx := context . TODO ()
138+ ctx := t . Context ()
134139 ids , err := d .Submit (ctx , msgs , 0 , testNamespace )
135140 assert .NoError (t , err )
136141 assert .Len (t , ids , len (msgs ))
@@ -140,9 +145,12 @@ func GetIDsTest(t *testing.T, d coreda.DA) {
140145 // To Keep It Simple: we assume working with DA used exclusively for this test (mock, devnet, etc)
141146 // As we're the only user, we don't need to handle external data (that could be submitted in real world).
142147 // There is no notion of height, so we need to scan the DA to get test data back.
143- for i := uint64 (1 ); ! found && ! time .Now ().After (end ); i ++ {
148+ for i := uint64 (0 ); ! found && ! time .Now ().After (end ); i ++ {
144149 ret , err := d .GetIDs (ctx , i , []byte {})
145150 if err != nil {
151+ if strings .Contains (err .Error (), coreda .ErrHeightFromFuture .Error ()) {
152+ break
153+ }
146154 t .Error ("failed to get IDs:" , err )
147155 }
148156 assert .NotNil (t , ret )
@@ -170,7 +178,7 @@ func GetIDsTest(t *testing.T, d coreda.DA) {
170178// ConcurrentReadWriteTest tests the use of mutex lock in DummyDA by calling separate methods that use `d.data` and making sure there's no race conditions
171179func ConcurrentReadWriteTest (t * testing.T , d coreda.DA ) {
172180 var wg sync.WaitGroup
173- ctx , cancel := context .WithTimeout (context . TODO (), 5 * time .Second )
181+ ctx , cancel := context .WithTimeout (t . Context (), 5 * time .Second )
174182 defer cancel ()
175183
176184 writeDone := make (chan struct {})
@@ -179,7 +187,7 @@ func ConcurrentReadWriteTest(t *testing.T, d coreda.DA) {
179187 go func () {
180188 defer wg .Done ()
181189 for i := uint64 (1 ); i <= 50 ; i ++ {
182- _ , err := d .Submit (ctx , []coreda.Blob {[]byte (fmt .Sprintf ("test-%d" , i ))}, 0 , []byte {} )
190+ _ , err := d .Submit (ctx , []coreda.Blob {[]byte (fmt .Sprintf ("test-%d" , i ))}, 0 , []byte ( "test" ) )
183191 assert .NoError (t , err )
184192 }
185193 close (writeDone )
@@ -193,10 +201,7 @@ func ConcurrentReadWriteTest(t *testing.T, d coreda.DA) {
193201 case <- writeDone :
194202 return
195203 default :
196- ret , err := d .GetIDs (ctx , 1 , []byte ("test" ))
197- if err != nil {
198- assert .Empty (t , ret .IDs )
199- }
204+ d .GetIDs (ctx , 0 , []byte ("test" ))
200205 }
201206 }
202207 }()
@@ -206,11 +211,11 @@ func ConcurrentReadWriteTest(t *testing.T, d coreda.DA) {
206211
207212// HeightFromFutureTest tests the case when the given height is from the future
208213func HeightFromFutureTest (t * testing.T , d coreda.DA ) {
209- ctx := context . TODO ()
214+ ctx := t . Context ()
210215 _ , err := d .GetIDs (ctx , 999999999 , []byte ("test" ))
211216 assert .Error (t , err )
212- // Specifically check if the error is ErrBlobNotFound (or contains its message)
213- assert .ErrorContains (t , err , coreda .ErrBlobNotFound .Error ())
217+ // Specifically check if the error contains the error message ErrHeightFromFuture
218+ assert .ErrorContains (t , err , coreda .ErrHeightFromFuture .Error ())
214219}
215220
216221// TestSubmitWithOptions tests the SubmitWithOptions method with various scenarios
0 commit comments