github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/internal/caching/pcache_test.go (about)

     1  /*
     2   * Copyright 2021 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package caching
    18  
    19  import (
    20  	"sync"
    21  	"testing"
    22  
    23  	"github.com/goshafaq/sonic/internal/rt"
    24  )
    25  
    26  func TestPcacheRace(t *testing.T) {
    27  	t.Parallel()
    28  
    29  	pc := CreateProgramCache()
    30  	wg := sync.WaitGroup{}
    31  	wg.Add(2)
    32  	start := make(chan struct{}, 2)
    33  
    34  	go func() {
    35  		defer wg.Done()
    36  		var k = map[string]interface{}{}
    37  		<-start
    38  		for i := 0; i < 100; i++ {
    39  			_, _ = pc.Compute(rt.UnpackEface(k).Type, func(*rt.GoType, ...interface{}) (interface{}, error) {
    40  				return map[string]interface{}{}, nil
    41  			})
    42  		}
    43  	}()
    44  
    45  	go func() {
    46  		defer wg.Done()
    47  		var k = map[string]interface{}{}
    48  		<-start
    49  		for i := 0; i < 100; i++ {
    50  			pc.Get(rt.UnpackEface(k).Type)
    51  		}
    52  	}()
    53  
    54  	start <- struct{}{}
    55  	start <- struct{}{}
    56  	wg.Wait()
    57  }