-
Notifications
You must be signed in to change notification settings - Fork 6
/
err2_test.go
1011 lines (903 loc) · 21.2 KB
/
err2_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package err2_test
import (
"fmt"
"io"
"os"
"runtime"
"testing"
"github.com/lainio/err2"
"github.com/lainio/err2/internal/test"
"github.com/lainio/err2/try"
)
const errStringInThrow = "this is an ERROR"
func throw() (string, error) {
return "", fmt.Errorf(errStringInThrow)
}
func twoStrNoThrow() (string, string, error) { return "test", "test", nil }
func intStrNoThrow() (int, string, error) { return 1, "test", nil }
func boolIntStrNoThrow() (bool, int, string, error) { return true, 1, "test", nil }
func noThrow() (string, error) { return "test", nil }
func noErr() error {
return nil
}
func TestTry_noError(t *testing.T) {
t.Parallel()
try.To1(noThrow())
try.To2(twoStrNoThrow())
try.To2(intStrNoThrow())
try.To3(boolIntStrNoThrow())
}
func TestDefault_Error(t *testing.T) {
t.Parallel()
var err error
defer err2.Handle(&err)
try.To1(throw())
t.Fail() // If everything works we are never here
}
func TestTry_Error(t *testing.T) {
t.Parallel()
var err error
defer err2.Handle(&err, func(err error) error { return err })
try.To1(throw())
t.Fail() // If everything works we are never here
}
func TestHandle_noerrHandler(t *testing.T) {
t.Parallel()
t.Run("noerr handler in ONLY one and NO error happens", func(t *testing.T) {
t.Parallel()
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
}()
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
handlerCalled = noerr
})
try.To(noErr())
})
t.Run("noerr handler is the last and NO error happens", func(t *testing.T) {
t.Parallel()
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
}()
defer err2.Handle(&err, func(err error) error {
// this should not be called, so lets try to fuckup things...
handlerCalled = false
test.Require(t, false)
return err
})
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
handlerCalled = noerr
})
try.To(noErr())
})
t.Run("noerr handler is the last and error happens", func(t *testing.T) {
t.Parallel()
var err error
var handlerCalled bool
defer func() {
test.Require(t, !handlerCalled)
}()
defer err2.Handle(&err, func(err error) error {
handlerCalled = false
test.Require(t, true, "error should be handled")
return err
})
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, noerr)
handlerCalled = noerr
})
try.To1(throw())
})
t.Run("noerr is first and error happens with many handlers", func(t *testing.T) {
t.Parallel()
var (
err error
finalAnnotatedErr = fmt.Errorf("err: %v", errStringInThrow)
handlerCalled bool
)
defer func() {
test.Require(t, !handlerCalled)
test.RequireEqual(t, err.Error(), finalAnnotatedErr.Error())
}()
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, false, "if error occurs/reset, this cannot happen")
handlerCalled = noerr
})
// important! test that our handler doesn't change the current error
// and it's not nil
defer err2.Handle(&err, func(er error) error {
test.Require(t, er != nil, "er val: ", er, err)
return er
})
defer err2.Handle(&err, func(err error) error {
// this should not be called, so lets try to fuckup things...
handlerCalled = false
test.Require(t, err != nil)
return finalAnnotatedErr
})
try.To1(throw())
})
t.Run("noerr handler is first and NO error happens", func(t *testing.T) {
t.Parallel()
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
}()
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, noerr)
handlerCalled = noerr
})
defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
})
try.To(noErr())
})
t.Run("noerr handler is first of MANY and NO error happens", func(t *testing.T) {
t.Parallel()
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
}()
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, noerr)
handlerCalled = noerr
})
defer err2.Handle(&err)
defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
})
defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
})
try.To(noErr())
})
t.Run("noerr handler is first of MANY and error happens UNTIL RESET", func(t *testing.T) {
t.Parallel()
var err error
var noerrHandlerCalled, errHandlerCalled bool
defer func() {
test.Require(t, noerrHandlerCalled)
test.Require(t, errHandlerCalled)
}()
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, true) // we are here, for debugging
test.Require(t, noerr)
noerrHandlerCalled = noerr
})
// this is the err handler that -- RESETS -- the error to nil
defer err2.Handle(&err, func(err error) error {
test.Require(t, err != nil) // helps fast debugging
// this should not be called, so lets try to fuckup things...
noerrHandlerCalled = false // see first deferred function
// keep the track that we have been here
errHandlerCalled = true // see first deferred function
return nil
})
defer err2.Handle(&err, func(err error) error {
test.Require(t, err != nil) // helps fast debugging
// this should not be called, so lets try to fuckup things...
noerrHandlerCalled = false // see first deferred function
errHandlerCalled = true // see first deferred function
return err
})
try.To1(throw())
})
t.Run("noerr handler is middle of MANY and NO error happens", func(t *testing.T) {
t.Parallel()
var err error
var handlerCalled bool
defer func() {
test.Require(t, handlerCalled)
}()
defer err2.Handle(&err)
defer err2.Handle(&err)
defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
})
// This is the handler we are thesting!
defer err2.Handle(&err, func(noerr bool) {
test.Require(t, noerr)
handlerCalled = noerr
})
defer err2.Handle(&err, func(err error) error {
test.Require(t, false, "no error to handle!")
// this should not be called, so lets try to fuckup things...
handlerCalled = false // see first deferred function
return err
})
try.To(noErr())
})
}
func TestPanickingCatchAll(t *testing.T) {
t.Parallel()
type args struct {
f func()
}
tests := []struct {
name string
args args
wants error
}{
{"general panic",
args{
func() {
defer err2.Catch(
err2.Noop,
func(any) {},
)
panic("panic")
},
},
nil,
},
{"runtime.error panic",
args{
func() {
defer err2.Catch(
err2.Err(func(error) {}), // Using simplifier
func(any) {},
)
var b []byte
b[0] = 0
},
},
nil,
},
{"stop panic with empty catch",
args{
func() {
defer err2.Catch()
var b []byte
b[0] = 0
},
},
nil,
},
{"stop panic with error handler in catch",
args{
func() {
defer err2.Catch(err2.Noop)
var b []byte
b[0] = 0
},
},
nil,
},
}
for _, ttv := range tests {
tt := ttv
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
test.Require(t, recover() == nil, "panics should NOT carry on")
}()
tt.args.f()
})
}
}
func TestPanickingCarryOn_Handle(t *testing.T) {
t.Parallel()
type args struct {
f func()
}
tests := []struct {
name string
args args
wants error
}{
{"general panic",
args{
func() {
var err error
defer err2.Handle(&err, err2.Noop)
panic("panic")
},
},
nil,
},
{"runtime.error panic",
args{
func() {
var err error
defer err2.Handle(&err, err2.Noop)
var b []byte
b[0] = 0
},
},
nil,
},
}
for _, ttv := range tests {
tt := ttv
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
test.Require(t, recover() != nil, "panics should went thru when not our errors")
}()
tt.args.f()
})
}
}
func TestPanicking_Handle(t *testing.T) {
t.Parallel()
type args struct {
f func() (err error)
}
myErr := fmt.Errorf("my error")
annErr := fmt.Errorf("annotated: %w", myErr)
tests := []struct {
name string
args args
wants error
}{
{"general error thru panic with annotion handler",
args{
func() (err error) {
// If we want keep same error value second argument
// must be nil
defer err2.Handle(&err, func(err error) error {
return fmt.Errorf("annotated: %w", err)
})
try.To(myErr)
return nil
},
},
annErr,
},
{"general error thru panic: handle nil: no automatic",
args{
func() (err error) {
// If we want keep same error value second argument
// must be nil
defer err2.Handle(&err, nil)
try.To(myErr)
return nil
},
},
myErr,
},
{"general panic",
args{
func() (err error) {
defer err2.Handle(&err)
panic("panic")
},
},
nil,
},
{"general panic plus err handler",
args{
func() (err error) {
defer err2.Handle(&err, err2.Noop)
panic("panic")
},
},
nil,
},
{"general panic stoped with handler plus err handler",
args{
func() (err error) {
defer err2.Handle(&err,
func(err error) error { return err },
func(any) {},
)
panic("panic")
},
},
myErr,
},
{"general panic stoped with handler",
args{
func() (err error) {
defer err2.Handle(&err, func(any) {})
panic("panic")
},
},
myErr,
},
{"general panic stoped with handler plus fmt string",
args{
func() (err error) {
defer err2.Handle(&err, func(any) {}, "string")
panic("panic")
},
},
myErr,
},
{"runtime.error panic",
args{
func() (err error) {
defer err2.Handle(&err)
var b []byte
b[0] = 0
return nil
},
},
nil,
},
{"runtime.error panic stopped with handler",
args{
func() (err error) {
defer err2.Handle(&err, func(any) {})
var b []byte
b[0] = 0
return nil
},
},
myErr,
},
}
for _, ttv := range tests {
tt := ttv
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if tt.wants == nil {
test.Require(t, r != nil, "wants err, then panic")
}
}()
err := tt.args.f()
if err != nil {
test.RequireEqual(t, err.Error(), tt.wants.Error())
}
})
}
}
func TestPanicking_Catch(t *testing.T) {
t.Parallel()
type args struct {
f func()
}
tests := []struct {
name string
args args
wants error
}{
{"general panic",
args{
func() {
defer err2.Catch(err2.Noop)
panic("panic")
},
},
nil,
},
{"runtime.error panic",
args{
func() {
defer err2.Catch(err2.Noop)
var b []byte
b[0] = 0
},
},
nil,
},
}
for _, ttv := range tests {
tt := ttv
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
defer func() {
test.Require(t, recover() == nil, "panics should NOT carry on")
}()
tt.args.f()
})
}
}
func TestCatch_Error(t *testing.T) {
t.Parallel()
defer err2.Catch()
try.To1(throw())
t.Fail() // If everything works we are never here
}
func Test_TryOutError(t *testing.T) {
t.Parallel()
defer err2.Catch(func(err error) error {
test.RequireEqual(t, err.Error(), "fails: test: this is an ERROR",
"=> we should catch right error str here")
return err
})
var retVal string
// let's test try.Out1() and it's throw capabilities here, even try.To1()
// is the preferred way.
retVal = try.Out1(noThrow()).Handle().Val1
test.Require(t, retVal == "test", "if no error happens, we get value")
_ = try.Out1(throw()).Handle("fails: %v", retVal).Val1
t.Fail() // If everything works in Handle we are never here.
}
func TestCatch_Panic(t *testing.T) {
t.Parallel()
panicHandled := false
defer func() {
// when err2.Catch's panic handler works fine, panic is handled
if !panicHandled {
t.Fail()
}
}()
defer err2.Catch(
func(error) error {
t.Log("it was panic, not an error")
t.Fail() // we should not be here
return nil
},
func(any) {
panicHandled = true
})
panic("test panic")
}
func TestSetErrorTracer(t *testing.T) {
t.Parallel()
w := err2.ErrorTracer()
test.Require(t, w == nil, "error tracer should be nil")
var w1 io.Writer
err2.SetErrorTracer(w1)
w = err2.ErrorTracer()
test.Require(t, w == nil, "error tracer should be nil")
}
func ExampleCatch_withFmt() {
// Set default logger to stdout for this example
oldLogW := err2.LogTracer()
err2.SetLogTracer(os.Stdout)
defer err2.SetLogTracer(oldLogW)
transport := func() {
// See how Catch follows given format string similarly as Handle
defer err2.Catch("catch")
err2.Throwf("our error")
}
transport()
// Output: catch: our error
}
func ExampleHandle() {
var err error
defer err2.Handle(&err)
try.To1(noThrow())
// Output:
}
func ExampleHandle_errThrow() {
transport := func() (err error) {
defer err2.Handle(&err)
err2.Throwf("our error")
return nil
}
err := transport()
fmt.Printf("%v", err)
// Output: testing: run example: our error
}
func ExampleHandle_annotatedErrReturn() {
normalReturn := func() (err error) {
defer err2.Handle(&err) // automatic annotation
return fmt.Errorf("our error")
}
err := normalReturn()
fmt.Printf("%v", err)
// ------- func name comes from Go example/test harness
// ------- v ------------------ v --------
// Output: testing: run example: our error
}
func ExampleHandle_errReturn() {
normalReturn := func() (err error) {
defer err2.Handle(&err, nil) // nil disables automatic annotation
return fmt.Errorf("our error")
}
err := normalReturn()
fmt.Printf("%v", err)
// Output: our error
}
func ExampleHandle_empty() {
annotated := func() (err error) {
defer err2.Handle(&err, "annotated")
try.To1(throw())
return err
}
err := annotated()
fmt.Printf("%v", err)
// Output: annotated: this is an ERROR
}
func ExampleHandle_annotate() {
annotated := func() (err error) {
defer err2.Handle(&err, "annotated: %s", "err2")
try.To1(throw())
return err
}
err := annotated()
fmt.Printf("%v", err)
// Output: annotated: err2: this is an ERROR
}
func ExampleThrowf() {
type fn func(v int) int
var recursion fn
const recursionLimit = 77 // 12+11+10+9+8+7+6+5+4+3+2+1 = 78
recursion = func(i int) int {
if i > recursionLimit { // simulated error case
err2.Throwf("helper failed at: %d", i)
} else if i == 0 {
return 0 // recursion without error ends here
}
return i + recursion(i-1)
}
annotated := func() (err error) {
defer err2.Handle(&err, "annotated: %s", "err2")
r := recursion(12) // call recursive algorithm successfully
recursion(r) // call recursive algorithm unsuccessfully
return err
}
err := annotated()
fmt.Printf("%v", err)
// Output: annotated: err2: helper failed at: 78
}
func ExampleHandle_deferStack() {
annotated := func() (err error) {
defer err2.Handle(&err, "annotated 2nd")
defer err2.Handle(&err, "annotated 1st")
try.To1(throw())
return err
}
err := annotated()
fmt.Printf("%v", err)
// Output: annotated 2nd: annotated 1st: this is an ERROR
}
func ExampleHandle_handlerFn() {
doSomething := func(a, b int) (err error) {
defer err2.Handle(&err, func(err error) error {
// Example for just annotating current err. Normally Handle is
// used for e.g. cleanup, not annotation that can be left for
// err2 automatic annotation. See CopyFile example for more
// information.
return fmt.Errorf("error with (%d, %d): %v", a, b, err)
})
try.To1(throw())
return err
}
err := doSomething(1, 2)
fmt.Printf("%v", err)
// Output: error with (1, 2): this is an ERROR
}
func ExampleHandle_multipleHandlerFns() {
doSomething := func(a, b int) (err error) {
defer err2.Handle(&err,
// cause automatic annotation <== 2 error handlers do the trick
err2.Noop,
func(err error) error {
// Example for just annotating current err. Normally Handle
// is used for e.g. cleanup, not annotation that can be left
// for err2 automatic annotation. See CopyFile example for
// more information.
return fmt.Errorf("%w error with (%d, %d)", err, a, b)
})
try.To1(throw())
return err
}
err := doSomething(1, 2)
fmt.Printf("%v", err)
// Output: testing: run example: this is an ERROR error with (1, 2)
}
func ExampleHandle_noThrow() {
doSomething := func(a, b int) (err error) {
defer err2.Handle(&err, func(err error) error {
return fmt.Errorf("error with (%d, %d): %v", a, b, err)
})
try.To1(noThrow())
return err
}
err := doSomething(1, 2)
fmt.Printf("%v", err)
// Output: <nil>
}
func BenchmarkOldErrorCheckingWithIfClause(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := noThrow()
if err != nil {
return
}
}
}
func BenchmarkTry_ErrVar(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := noThrow()
try.To(err)
}
}
func BenchmarkTryOut_ErrVar(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := noThrow()
try.Out(err).Handle()
}
}
func BenchmarkTry_StringGenerics(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = try.To1(noThrow())
}
}
func BenchmarkTryOut_StringGenerics(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = try.Out1(noThrow()).Handle()
}
}
func BenchmarkTry_StrStrGenerics(b *testing.B) {
for n := 0; n < b.N; n++ {
_, _ = try.To2(twoStrNoThrow())
}
}
func BenchmarkTryInsideCall(b *testing.B) {
for n := 0; n < b.N; n++ {
try.To(noErr())
}
}
func BenchmarkTryVarCall(b *testing.B) {
for n := 0; n < b.N; n++ {
err := noErr()
try.To(err)
}
}
func BenchmarkRecursionWithOldErrorCheck(b *testing.B) {
var recursionWithErrorCheck func(a int) (int, error)
recursionWithErrorCheck = func(a int) (int, error) {
if a == 0 {
return 0, nil
}
s, err := noThrow()
if err != nil {
return 0, err
}
_ = s
v, err := recursionWithErrorCheck(a - 1)
if err != nil {
return 0, err
}
return a + v, nil
}
for n := 0; n < b.N; n++ {
_, err := recursionWithErrorCheck(100)
if err != nil {
return
}
}
}
func BenchmarkRecursionWithOldErrorIfCheckAnd_Defer(b *testing.B) {
var recursionWithErrorCheckAndDefer func(a int) (_ int, err error)
recursionWithErrorCheckAndDefer = func(a int) (_ int, err error) {
defer err2.Handle(&err)
if a == 0 {
return 0, nil
}
s, err := noThrow()
if err != nil {
return 0, err
}
_ = s
v, err := recursionWithErrorCheckAndDefer(a - 1)
if err != nil {
return 0, err
}
return a + v, nil
}
for n := 0; n < b.N; n++ {
_, err := recursionWithErrorCheckAndDefer(100)
if err != nil {
return
}
}
}
func BenchmarkRecursionWithTryCall(b *testing.B) {
var cleanRecursion func(a int) int
cleanRecursion = func(a int) int {
if a == 0 {
return 0
}
s := try.To1(noThrow())
_ = s
return a + cleanRecursion(a-1)
}
for n := 0; n < b.N; n++ {
_ = cleanRecursion(100)
}
}
func BenchmarkRecursionWithTryAnd_Empty_Defer(b *testing.B) {
var recursion func(a int) (r int, err error)
recursion = func(a int) (r int, err error) {
defer func(e error) { // try to be as close to our case, but simple!
err = e
}(err)
if a == 0 {
return 0, nil
}
s := try.To1(noThrow())
_ = s
r = try.To1(recursion(a - 1))
r += a
return r, nil
}
for n := 0; n < b.N; n++ {
_, _ = recursion(100)
}
}
func doWork(ePtr *error, r any) {
switch v := r.(type) {
case nil:
return
case runtime.Error:
*ePtr = fmt.Errorf("%v: %w", *ePtr, v)
case error:
*ePtr = fmt.Errorf("%v: %w", *ePtr, v)
default:
// panicing
}
}
// Next benchmark is only for internal test for trying to reproduce Go compilers
// missing optimization behavior.
func BenchmarkRecursionWithTryAnd_HeavyPtrPtr_Defer(b *testing.B) {
var recursion func(a int) (r int, err error)
recursion = func(a int) (r int, err error) {
defer func(ePtr *error) {
r := recover()
nothingToDo := r == nil && (ePtr == nil || *ePtr == nil)
if nothingToDo {
return
}
doWork(ePtr, r)
}(&err)
if a == 0 {
return 0, nil
}
s := try.To1(noThrow())
_ = s
r = try.To1(recursion(a - 1))
r += a
return r, nil
}
for n := 0; n < b.N; n++ {
_, _ = recursion(100)
}
}
func BenchmarkRecursionWithTryAndDefer(b *testing.B) {
var recursion func(a int) (r int, err error)
recursion = func(a int) (r int, err error) {
defer err2.Handle(&err)
if a == 0 {
return 0, nil
}
s := try.To1(noThrow())
_ = s
r = try.To1(recursion(a - 1))
r += a
return r, nil
}
for n := 0; n < b.N; n++ {
_, _ = recursion(100)
}
}
func TestMain(m *testing.M) {