github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/kafka/correlation_cache_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package kafka
    18  
    19  import (
    20  	"time"
    21  
    22  	. "gopkg.in/check.v1"
    23  )
    24  
    25  var (
    26  	request1 = &RequestMessage{rawMsg: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}
    27  	request2 = &RequestMessage{rawMsg: []byte{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}}
    28  )
    29  
    30  func createResponse(req *RequestMessage) *ResponseMessage {
    31  	res := &ResponseMessage{rawMsg: req.rawMsg}
    32  	res.SetCorrelationID(req.GetCorrelationID())
    33  	return res
    34  }
    35  
    36  func (k *kafkaTestSuite) TestCorrelation(c *C) {
    37  	cc := NewCorrelationCache()
    38  
    39  	req1FinishCalled, req2FinishCalled := false, false
    40  
    41  	// save the original correlation ID for later checking
    42  	origCorrelationID := request1.GetCorrelationID()
    43  
    44  	cc.HandleRequest(request1, func(req *RequestMessage) { req1FinishCalled = true })
    45  
    46  	// Verify that the correlation ID has been rewritten to the next
    47  	// sequence number (1)
    48  	c.Assert(request1.GetCorrelationID(), Equals, CorrelationID(1))
    49  
    50  	// Successful correlation will remove the request from the cache so
    51  	// subsequent correlation will return nil
    52  	response1 := createResponse(request1)
    53  	c.Assert(cc.CorrelateResponse(response1), Equals, request1)
    54  	c.Assert(cc.CorrelateResponse(response1), IsNil)
    55  
    56  	// Verify that the correlation id in the response has been restored to
    57  	// the original value found in the request
    58  	c.Assert(response1.GetCorrelationID(), Equals, origCorrelationID)
    59  
    60  	cc.HandleRequest(request2, nil)
    61  
    62  	// Verify that the correlation ID has been rewritten to the next
    63  	// sequence number (2)
    64  	c.Assert(request2.GetCorrelationID(), Equals, CorrelationID(2))
    65  
    66  	response2 := createResponse(request2)
    67  	c.Assert(cc.CorrelateResponse(response2), Equals, request2)
    68  	c.Assert(cc.CorrelateResponse(response2), IsNil)
    69  
    70  	// Check that only finish function of request was called as request2
    71  	// did not have a finish function attached
    72  	c.Assert(req1FinishCalled, Equals, true)
    73  	c.Assert(req2FinishCalled, Equals, false)
    74  
    75  	cc.DeleteCache()
    76  }
    77  
    78  func (k *kafkaTestSuite) TestCorrelationGC(c *C) {
    79  	// reduce the lifetime of a request in the cache to 200 millisecond
    80  	RequestLifetime = 200 * time.Millisecond
    81  
    82  	cc := NewCorrelationCache()
    83  
    84  	// sleep into half the request lifetime interval
    85  	time.Sleep(100 * time.Millisecond)
    86  
    87  	// Let initial GC run complete
    88  	for cc.numGcRuns < 1 {
    89  		time.Sleep(1 * time.Millisecond)
    90  	}
    91  
    92  	cc.HandleRequest(request1, nil)
    93  
    94  	// Let another GC run pass
    95  	for cc.numGcRuns < 2 {
    96  		time.Sleep(1 * time.Millisecond)
    97  	}
    98  
    99  	// request1 should not have been expired yet
   100  	response1 := createResponse(request1)
   101  	c.Assert(cc.correlate(response1.GetCorrelationID()), Not(IsNil))
   102  
   103  	// wait for the garbage collector to expire the request
   104  	for cc.numExpired == 0 {
   105  		time.Sleep(1 * time.Millisecond)
   106  	}
   107  
   108  	// Garbage collector must have removed the request from the cache
   109  	c.Assert(cc.CorrelateResponse(response1), IsNil)
   110  	response2 := createResponse(request2)
   111  	c.Assert(cc.CorrelateResponse(response2), IsNil)
   112  
   113  	cc.DeleteCache()
   114  }