@@ -817,6 +817,129 @@ func TestChunkReporter(t *testing.T) {
817817 })
818818}
819819
820+ func TestDeleteTagReporter (t * testing.T ) {
821+
822+ t .Parallel ()
823+
824+ ts := newTestStorage (t )
825+
826+ var (
827+ tag upload.TagItem
828+ putter internal.PutterCloserWithReference
829+ err error
830+ )
831+
832+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
833+ tag , err = upload .NextTag (s .IndexStore ())
834+ return err
835+ }); err != nil {
836+ t .Fatalf ("failed creating tag: %v" , err )
837+ }
838+
839+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
840+ putter , err = upload .NewPutter (s .IndexStore (), tag .TagID )
841+ return err
842+ }); err != nil {
843+ t .Fatalf ("failed creating putter: %v" , err )
844+ }
845+
846+ t .Run ("delete tag while uploading" , func (t * testing.T ) {
847+
848+ chunk := chunktest .GenerateTestRandomChunks (1 )[0 ]
849+
850+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
851+ return putter .Put (context .Background (), s , chunk )
852+ }); err != nil {
853+ t .Fatalf ("Put(...): unexpected error: %v" , err )
854+ }
855+
856+ report := func (ch swarm.Chunk , state int ) {
857+ t .Helper ()
858+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
859+ return upload .Report (context .Background (), s , ch , state )
860+ }); err != nil {
861+ t .Fatalf ("Report(...): unexpected error: %v" , err )
862+ }
863+ }
864+
865+ tagItem := & upload.TagItem {TagID : tag .TagID }
866+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
867+ return s .IndexStore ().Get (tagItem )
868+ }); err != nil {
869+ t .Fatalf ("Get(...): unexpected error: %v" , err )
870+ }
871+
872+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
873+ return s .IndexStore ().Delete (tagItem )
874+ }); err != nil {
875+ t .Fatalf ("Put(...): unexpected error: %v" , err )
876+ }
877+
878+ t .Run ("mark sent" , func (t * testing.T ) {
879+ report (chunk , storage .ChunkSent )
880+ })
881+
882+ t .Run ("verify internal state" , func (t * testing.T ) {
883+
884+ ui := & upload.UploadItem {Address : chunk .Address (), BatchID : chunk .Stamp ().BatchID ()}
885+ err := ts .IndexStore ().Get (ui )
886+ if err != nil {
887+ t .Fatalf ("Report(...): unexpected error: %v" , err )
888+ }
889+
890+ if diff := cmp .Diff (uint64 (0 ), ui .TagID ); diff != "" {
891+ t .Fatalf ("Get(...): unexpected TagItem (-want +have):\n %s" , diff )
892+ }
893+ })
894+ })
895+
896+ t .Run ("delete tag while uploading and not sent" , func (t * testing.T ) {
897+ // Generate a chunk.
898+ chunk := chunktest .GenerateTestRandomChunks (1 )[0 ]
899+
900+ // Store the chunk (which creates the uploadItem).
901+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
902+ return putter .Put (context .Background (), s , chunk )
903+ }); err != nil {
904+ t .Fatalf ("Put(...): unexpected error: %v" , err )
905+ }
906+
907+ // Confirm the upload item exists.
908+ ui := & upload.UploadItem {
909+ Address : chunk .Address (),
910+ BatchID : chunk .Stamp ().BatchID (),
911+ }
912+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
913+ return s .IndexStore ().Get (ui )
914+ }); err != nil {
915+ t .Fatalf ("Get(...): unexpected error: %v" , err )
916+ }
917+
918+ // Delete the tag item to simulate a user deleting it.
919+ tagItem := & upload.TagItem {TagID : tag .TagID }
920+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
921+ return s .IndexStore ().Delete (tagItem )
922+ }); err != nil {
923+ t .Fatalf ("Delete(...): unexpected error: %v" , err )
924+ }
925+
926+ // Now report with a state other than ChunkSent, e.g. ChunkStored.
927+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
928+ return upload .Report (context .Background (), s , chunk , storage .ChunkStored )
929+ }); err != nil {
930+ t .Fatalf ("Report(...): unexpected error: %v" , err )
931+ }
932+
933+ // Verify that the upload item was deleted (cleanup via deleteFunc).
934+ if err := ts .Run (context .Background (), func (s transaction.Store ) error {
935+ return s .IndexStore ().Get (ui )
936+ }); ! errors .Is (err , storage .ErrNotFound ) {
937+ t .Fatalf ("expected uploadItem to be deleted, got error: %v" , err )
938+ }
939+ })
940+
941+ }
942+
820943func TestNextTagID (t * testing.T ) {
821944 t .Parallel ()
822945
0 commit comments