go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/quota/internal/luatest/testdata/utils_test.lua (about)

     1  -- Copyright 2022 The LUCI Authors
     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  
    16  require 'test_fixtures'
    17  
    18  Account = G.Account
    19  Policy = G.Policy
    20  Utils = G.Utils
    21  
    22  function testMillis()
    23    lu.assertEquals(Utils.Millis(nil), nil)
    24  
    25    local dur = PB.new("google.protobuf.Duration", {
    26      seconds = 127,
    27      nanos = 19304658,
    28    })
    29    lu.assertEquals(Utils.Millis(dur), 127019)
    30  end
    31  
    32  function testWithRequestIDNoKey()
    33    local req = PB.new("go.chromium.org.luci.server.quota.quotapb.UpdateAccountsInput")
    34  
    35    local ret = Utils.WithRequestID(req, function()
    36      return PB.new("google.protobuf.Duration", {
    37        seconds = 100,
    38      }), true
    39    end)
    40    lu.assertEquals(ret, "\145d")
    41    lu.assertEquals(redis.DATA, {})
    42    lu.assertEquals(redis.TTL, {})
    43  end
    44  
    45  function testWithRequestIDWithKeyAndHash()
    46    local req = PB.new("go.chromium.org.luci.server.quota.quotapb.UpdateAccountsInput", {
    47      request_key = "thing",
    48      request_key_ttl = PB.new("google.protobuf.Duration", {
    49        seconds = 127,
    50        nanos = 19304658,
    51      }),
    52      hash_scheme = 1,
    53      hash = "i am a banana.",
    54    })
    55  
    56    local ret = Utils.WithRequestID(req, function()
    57      return PB.new("google.protobuf.Duration", {
    58        seconds = 100,
    59      }), true
    60    end)
    61    lu.assertEquals(ret, "\145d")
    62  
    63    lu.assertEquals(redis.DATA["thing"], {
    64      hash="i am a banana.",
    65      hash_scheme="1",
    66      response="\145d",
    67    })
    68    lu.assertEquals(redis.TTL["thing"], 127019)
    69  
    70    local ret = Utils.WithRequestID(req, function()
    71      error("no! this should be deduplicated!")
    72    end)
    73    lu.assertEquals(ret, "\145d")
    74  
    75    req.hash = "not banana"
    76    local ret = Utils.WithRequestID(req, function()
    77      error("no! this should be deduplicated!")
    78    end)
    79    lu.assertEquals(ret, {
    80      error = "ERR REQUEST_HASH",
    81    })
    82  
    83    req.hash_scheme = 2 -- allowed
    84    local ret = Utils.WithRequestID(req, function()
    85      error("no! this should be deduplicated!")
    86    end)
    87    lu.assertEquals(ret, "\145d")
    88  end
    89  
    90  function testWithRequestIDError()
    91    local req = PB.new("go.chromium.org.luci.server.quota.quotapb.UpdateAccountsInput", {
    92      request_key = "thing",
    93      request_key_ttl = PB.new("google.protobuf.Duration", {
    94        seconds = 127,
    95        nanos = 19304658,
    96      }),
    97      hash_scheme = 1,
    98      hash = "i am a banana.",
    99    })
   100  
   101    local ret = Utils.WithRequestID(req, function()
   102      return PB.new("google.protobuf.Duration", {
   103        seconds = 100,
   104      }), false
   105    end)
   106    lu.assertEquals(ret, "\145d")
   107    lu.assertEquals(redis.DATA, {})
   108    lu.assertEquals(redis.TTL, {})
   109  end
   110  
   111  return lu.LuaUnit.run("-v")