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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
|
//go:build validatedebug
package validate
import (
"fmt"
"runtime"
"sync"
"testing"
"github.com/go-openapi/spec"
)
// This version of the pools is to be used for debugging and testing, with build tag "validatedebug".
//
// In this mode, the pools are tracked for allocation and redemption of borrowed objects, so we can
// verify a few behaviors of the validators. The debug pools panic when an invalid usage pattern is detected.
var pools allPools
func init() {
resetPools()
}
func resetPools() {
// NOTE: for testing purpose, we might want to reset pools after calling Validate twice.
// The pool is corrupted in that case: calling Put twice inserts a duplicate in the pool
// and further calls to Get are mishandled.
pools = allPools{
poolOfSchemaValidators: schemaValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &SchemaValidator{}
return s
},
},
debugMap: make(map[*SchemaValidator]status),
allocMap: make(map[*SchemaValidator]string),
redeemMap: make(map[*SchemaValidator]string),
},
poolOfObjectValidators: objectValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &objectValidator{}
return s
},
},
debugMap: make(map[*objectValidator]status),
allocMap: make(map[*objectValidator]string),
redeemMap: make(map[*objectValidator]string),
},
poolOfSliceValidators: sliceValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &schemaSliceValidator{}
return s
},
},
debugMap: make(map[*schemaSliceValidator]status),
allocMap: make(map[*schemaSliceValidator]string),
redeemMap: make(map[*schemaSliceValidator]string),
},
poolOfItemsValidators: itemsValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &itemsValidator{}
return s
},
},
debugMap: make(map[*itemsValidator]status),
allocMap: make(map[*itemsValidator]string),
redeemMap: make(map[*itemsValidator]string),
},
poolOfBasicCommonValidators: basicCommonValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &basicCommonValidator{}
return s
},
},
debugMap: make(map[*basicCommonValidator]status),
allocMap: make(map[*basicCommonValidator]string),
redeemMap: make(map[*basicCommonValidator]string),
},
poolOfHeaderValidators: headerValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &HeaderValidator{}
return s
},
},
debugMap: make(map[*HeaderValidator]status),
allocMap: make(map[*HeaderValidator]string),
redeemMap: make(map[*HeaderValidator]string),
},
poolOfParamValidators: paramValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &ParamValidator{}
return s
},
},
debugMap: make(map[*ParamValidator]status),
allocMap: make(map[*ParamValidator]string),
redeemMap: make(map[*ParamValidator]string),
},
poolOfBasicSliceValidators: basicSliceValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &basicSliceValidator{}
return s
},
},
debugMap: make(map[*basicSliceValidator]status),
allocMap: make(map[*basicSliceValidator]string),
redeemMap: make(map[*basicSliceValidator]string),
},
poolOfNumberValidators: numberValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &numberValidator{}
return s
},
},
debugMap: make(map[*numberValidator]status),
allocMap: make(map[*numberValidator]string),
redeemMap: make(map[*numberValidator]string),
},
poolOfStringValidators: stringValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &stringValidator{}
return s
},
},
debugMap: make(map[*stringValidator]status),
allocMap: make(map[*stringValidator]string),
redeemMap: make(map[*stringValidator]string),
},
poolOfSchemaPropsValidators: schemaPropsValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &schemaPropsValidator{}
return s
},
},
debugMap: make(map[*schemaPropsValidator]status),
allocMap: make(map[*schemaPropsValidator]string),
redeemMap: make(map[*schemaPropsValidator]string),
},
poolOfFormatValidators: formatValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &formatValidator{}
return s
},
},
debugMap: make(map[*formatValidator]status),
allocMap: make(map[*formatValidator]string),
redeemMap: make(map[*formatValidator]string),
},
poolOfTypeValidators: typeValidatorsPool{
Pool: &sync.Pool{
New: func() any {
s := &typeValidator{}
return s
},
},
debugMap: make(map[*typeValidator]status),
allocMap: make(map[*typeValidator]string),
redeemMap: make(map[*typeValidator]string),
},
poolOfSchemas: schemasPool{
Pool: &sync.Pool{
New: func() any {
s := &spec.Schema{}
return s
},
},
debugMap: make(map[*spec.Schema]status),
allocMap: make(map[*spec.Schema]string),
redeemMap: make(map[*spec.Schema]string),
},
poolOfResults: resultsPool{
Pool: &sync.Pool{
New: func() any {
s := &Result{}
return s
},
},
debugMap: make(map[*Result]status),
allocMap: make(map[*Result]string),
redeemMap: make(map[*Result]string),
},
}
}
const (
statusFresh status = iota + 1
statusRecycled
statusRedeemed
)
func (s status) String() string {
switch s {
case statusFresh:
return "fresh"
case statusRecycled:
return "recycled"
case statusRedeemed:
return "redeemed"
default:
panic(fmt.Errorf("invalid status: %d", s))
}
}
type (
// Debug
status uint8
allPools struct {
// memory pools for all validator objects.
//
// Each pool can be borrowed from and redeemed to.
poolOfSchemaValidators schemaValidatorsPool
poolOfObjectValidators objectValidatorsPool
poolOfSliceValidators sliceValidatorsPool
poolOfItemsValidators itemsValidatorsPool
poolOfBasicCommonValidators basicCommonValidatorsPool
poolOfHeaderValidators headerValidatorsPool
poolOfParamValidators paramValidatorsPool
poolOfBasicSliceValidators basicSliceValidatorsPool
poolOfNumberValidators numberValidatorsPool
poolOfStringValidators stringValidatorsPool
poolOfSchemaPropsValidators schemaPropsValidatorsPool
poolOfFormatValidators formatValidatorsPool
poolOfTypeValidators typeValidatorsPool
poolOfSchemas schemasPool
poolOfResults resultsPool
}
schemaValidatorsPool struct {
*sync.Pool
debugMap map[*SchemaValidator]status
allocMap map[*SchemaValidator]string
redeemMap map[*SchemaValidator]string
mx sync.Mutex
}
objectValidatorsPool struct {
*sync.Pool
debugMap map[*objectValidator]status
allocMap map[*objectValidator]string
redeemMap map[*objectValidator]string
mx sync.Mutex
}
sliceValidatorsPool struct {
*sync.Pool
debugMap map[*schemaSliceValidator]status
allocMap map[*schemaSliceValidator]string
redeemMap map[*schemaSliceValidator]string
mx sync.Mutex
}
itemsValidatorsPool struct {
*sync.Pool
debugMap map[*itemsValidator]status
allocMap map[*itemsValidator]string
redeemMap map[*itemsValidator]string
mx sync.Mutex
}
basicCommonValidatorsPool struct {
*sync.Pool
debugMap map[*basicCommonValidator]status
allocMap map[*basicCommonValidator]string
redeemMap map[*basicCommonValidator]string
mx sync.Mutex
}
headerValidatorsPool struct {
*sync.Pool
debugMap map[*HeaderValidator]status
allocMap map[*HeaderValidator]string
redeemMap map[*HeaderValidator]string
mx sync.Mutex
}
paramValidatorsPool struct {
*sync.Pool
debugMap map[*ParamValidator]status
allocMap map[*ParamValidator]string
redeemMap map[*ParamValidator]string
mx sync.Mutex
}
basicSliceValidatorsPool struct {
*sync.Pool
debugMap map[*basicSliceValidator]status
allocMap map[*basicSliceValidator]string
redeemMap map[*basicSliceValidator]string
mx sync.Mutex
}
numberValidatorsPool struct {
*sync.Pool
debugMap map[*numberValidator]status
allocMap map[*numberValidator]string
redeemMap map[*numberValidator]string
mx sync.Mutex
}
stringValidatorsPool struct {
*sync.Pool
debugMap map[*stringValidator]status
allocMap map[*stringValidator]string
redeemMap map[*stringValidator]string
mx sync.Mutex
}
schemaPropsValidatorsPool struct {
*sync.Pool
debugMap map[*schemaPropsValidator]status
allocMap map[*schemaPropsValidator]string
redeemMap map[*schemaPropsValidator]string
mx sync.Mutex
}
formatValidatorsPool struct {
*sync.Pool
debugMap map[*formatValidator]status
allocMap map[*formatValidator]string
redeemMap map[*formatValidator]string
mx sync.Mutex
}
typeValidatorsPool struct {
*sync.Pool
debugMap map[*typeValidator]status
allocMap map[*typeValidator]string
redeemMap map[*typeValidator]string
mx sync.Mutex
}
schemasPool struct {
*sync.Pool
debugMap map[*spec.Schema]status
allocMap map[*spec.Schema]string
redeemMap map[*spec.Schema]string
mx sync.Mutex
}
resultsPool struct {
*sync.Pool
debugMap map[*Result]status
allocMap map[*Result]string
redeemMap map[*Result]string
mx sync.Mutex
}
)
func (p *schemaValidatorsPool) BorrowValidator() *SchemaValidator {
s := p.Get().(*SchemaValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled schema should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *schemaValidatorsPool) RedeemValidator(s *SchemaValidator) {
// NOTE: s might be nil. In that case, Put is a noop.
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed schema should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed schema should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *objectValidatorsPool) BorrowValidator() *objectValidator {
s := p.Get().(*objectValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled object should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *objectValidatorsPool) RedeemValidator(s *objectValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed object should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed object should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *sliceValidatorsPool) BorrowValidator() *schemaSliceValidator {
s := p.Get().(*schemaSliceValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled schemaSliceValidator should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *sliceValidatorsPool) RedeemValidator(s *schemaSliceValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed schemaSliceValidator should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed schemaSliceValidator should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *itemsValidatorsPool) BorrowValidator() *itemsValidator {
s := p.Get().(*itemsValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled itemsValidator should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *itemsValidatorsPool) RedeemValidator(s *itemsValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed itemsValidator should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed itemsValidator should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *basicCommonValidatorsPool) BorrowValidator() *basicCommonValidator {
s := p.Get().(*basicCommonValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled basicCommonValidator should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *basicCommonValidatorsPool) RedeemValidator(s *basicCommonValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed basicCommonValidator should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed basicCommonValidator should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *headerValidatorsPool) BorrowValidator() *HeaderValidator {
s := p.Get().(*HeaderValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled HeaderValidator should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *headerValidatorsPool) RedeemValidator(s *HeaderValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed header should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed header should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *paramValidatorsPool) BorrowValidator() *ParamValidator {
s := p.Get().(*ParamValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled param should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *paramValidatorsPool) RedeemValidator(s *ParamValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed param should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed param should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *basicSliceValidatorsPool) BorrowValidator() *basicSliceValidator {
s := p.Get().(*basicSliceValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled basicSliceValidator should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *basicSliceValidatorsPool) RedeemValidator(s *basicSliceValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed basicSliceValidator should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed basicSliceValidator should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *numberValidatorsPool) BorrowValidator() *numberValidator {
s := p.Get().(*numberValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled number should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *numberValidatorsPool) RedeemValidator(s *numberValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed number should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed number should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *stringValidatorsPool) BorrowValidator() *stringValidator {
s := p.Get().(*stringValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled string should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *stringValidatorsPool) RedeemValidator(s *stringValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed string should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed string should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *schemaPropsValidatorsPool) BorrowValidator() *schemaPropsValidator {
s := p.Get().(*schemaPropsValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled param should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *schemaPropsValidatorsPool) RedeemValidator(s *schemaPropsValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed schemaProps should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed schemaProps should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *formatValidatorsPool) BorrowValidator() *formatValidator {
s := p.Get().(*formatValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled format should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *formatValidatorsPool) RedeemValidator(s *formatValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed format should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed format should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *typeValidatorsPool) BorrowValidator() *typeValidator {
s := p.Get().(*typeValidator)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled type should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *typeValidatorsPool) RedeemValidator(s *typeValidator) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed type should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic(fmt.Errorf("redeemed type should have been allocated from a fresh or recycled pointer. Got status %s, already redeamed at: %s", x, p.redeemMap[s]))
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *schemasPool) BorrowSchema() *spec.Schema {
s := p.Get().(*spec.Schema)
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled spec.Schema should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *schemasPool) RedeemSchema(s *spec.Schema) {
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed spec.Schema should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed spec.Schema should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *resultsPool) BorrowResult() *Result {
s := p.Get().(*Result).cleared()
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
p.debugMap[s] = statusFresh
} else {
if x != statusRedeemed {
panic("recycled result should have been redeemed")
}
p.debugMap[s] = statusRecycled
}
p.allocMap[s] = caller()
return s
}
func (p *resultsPool) RedeemResult(s *Result) {
if s == emptyResult {
if len(s.Errors) > 0 || len(s.Warnings) > 0 {
panic("empty result should not mutate")
}
return
}
p.mx.Lock()
defer p.mx.Unlock()
x, ok := p.debugMap[s]
if !ok {
panic("redeemed Result should have been allocated")
}
if x != statusRecycled && x != statusFresh {
panic("redeemed Result should have been allocated from a fresh or recycled pointer")
}
p.debugMap[s] = statusRedeemed
p.redeemMap[s] = caller()
p.Put(s)
}
func (p *allPools) allIsRedeemed(t testing.TB) bool {
outcome := true
for k, v := range p.poolOfSchemaValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("schemaValidator should be redeemed. Allocated by: %s", p.poolOfSchemaValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfObjectValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("objectValidator should be redeemed. Allocated by: %s", p.poolOfObjectValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfSliceValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("sliceValidator should be redeemed. Allocated by: %s", p.poolOfSliceValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfItemsValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("itemsValidator should be redeemed. Allocated by: %s", p.poolOfItemsValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfBasicCommonValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("basicCommonValidator should be redeemed. Allocated by: %s", p.poolOfBasicCommonValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfHeaderValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("headerValidator should be redeemed. Allocated by: %s", p.poolOfHeaderValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfParamValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("paramValidator should be redeemed. Allocated by: %s", p.poolOfParamValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfBasicSliceValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("basicSliceValidator should be redeemed. Allocated by: %s", p.poolOfBasicSliceValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfNumberValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("numberValidator should be redeemed. Allocated by: %s", p.poolOfNumberValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfStringValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("stringValidator should be redeemed. Allocated by: %s", p.poolOfStringValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfSchemaPropsValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("schemaPropsValidator should be redeemed. Allocated by: %s", p.poolOfSchemaPropsValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfFormatValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("formatValidator should be redeemed. Allocated by: %s", p.poolOfFormatValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfTypeValidators.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("typeValidator should be redeemed. Allocated by: %s", p.poolOfTypeValidators.allocMap[k])
outcome = false
}
for k, v := range p.poolOfSchemas.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("schemas should be redeemed. Allocated by: %s", p.poolOfSchemas.allocMap[k])
outcome = false
}
for k, v := range p.poolOfResults.debugMap {
if v == statusRedeemed {
continue
}
t.Logf("result should be redeemed. Allocated by: %s", p.poolOfResults.allocMap[k])
outcome = false
}
return outcome
}
func caller() string {
pc, _, _, _ := runtime.Caller(3) //nolint:dogsled
from, line := runtime.FuncForPC(pc).FileLine(pc)
return fmt.Sprintf("%s:%d", from, line)
}
|