|
| 1 | +/* |
| 2 | + * Flow Playground |
| 3 | + * |
| 4 | + * Copyright 2019 Dapper Labs, Inc. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package blockchain |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "github.com/dapperlabs/flow-playground-api/model" |
| 24 | + "github.com/google/uuid" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + "testing" |
| 28 | +) |
| 29 | + |
| 30 | +func createExecutions(count int) []*model.TransactionExecution { |
| 31 | + executions := make([]*model.TransactionExecution, count) |
| 32 | + for i := 0; i < count; i++ { |
| 33 | + executions[i] = &model.TransactionExecution{ |
| 34 | + ProjectChildID: model.NewProjectChildID(uuid.New(), uuid.New()), |
| 35 | + Index: i, |
| 36 | + Script: fmt.Sprintf(`transaction { execute { log(%d) } }`, i), |
| 37 | + } |
| 38 | + } |
| 39 | + return executions |
| 40 | +} |
| 41 | + |
| 42 | +func Test_Cache(t *testing.T) { |
| 43 | + |
| 44 | + t.Run("returns cached emulator", func(t *testing.T) { |
| 45 | + testID := uuid.New() |
| 46 | + c := newCache(2) |
| 47 | + |
| 48 | + em, err := newEmulator() |
| 49 | + require.NoError(t, err) |
| 50 | + |
| 51 | + c.add(testID, em) |
| 52 | + |
| 53 | + cacheEm, exe, err := c.get(testID, nil) |
| 54 | + require.NoError(t, err) |
| 55 | + assert.Len(t, exe, 0) |
| 56 | + |
| 57 | + cacheBlock, err := cacheEm.getLatestBlock() |
| 58 | + require.NoError(t, err) |
| 59 | + |
| 60 | + block, err := em.getLatestBlock() |
| 61 | + require.NoError(t, err) |
| 62 | + |
| 63 | + assert.Equal(t, block.ID(), cacheBlock.ID()) |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("returns cached emulator with executions", func(t *testing.T) { |
| 67 | + testID := uuid.New() |
| 68 | + c := newCache(2) |
| 69 | + |
| 70 | + em, err := newEmulator() |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + c.add(testID, em) |
| 74 | + |
| 75 | + executions := createExecutions(5) |
| 76 | + for _, exe := range executions { |
| 77 | + _, _, err := em.executeTransaction(exe.Script, exe.Arguments, nil) |
| 78 | + require.NoError(t, err) |
| 79 | + } |
| 80 | + |
| 81 | + cachedEm, cacheExe, err := c.get(testID, executions) |
| 82 | + require.NoError(t, err) |
| 83 | + // cached emulator contains all the executions |
| 84 | + assert.Len(t, cacheExe, 0) |
| 85 | + // make sure emulators are same |
| 86 | + cacheBlock, _ := cachedEm.getLatestBlock() |
| 87 | + block, _ := em.getLatestBlock() |
| 88 | + assert.Equal(t, cacheBlock.ID(), block.ID()) |
| 89 | + }) |
| 90 | + |
| 91 | + t.Run("returns cached emulator with missing executions", func(t *testing.T) { |
| 92 | + testID := uuid.New() |
| 93 | + c := newCache(2) |
| 94 | + |
| 95 | + em, err := newEmulator() |
| 96 | + require.NoError(t, err) |
| 97 | + |
| 98 | + c.add(testID, em) |
| 99 | + |
| 100 | + executions := createExecutions(5) |
| 101 | + |
| 102 | + for i, exe := range executions { |
| 103 | + if i == 3 { |
| 104 | + break // miss last two executions |
| 105 | + } |
| 106 | + _, _, err := em.executeTransaction(exe.Script, exe.Arguments, nil) |
| 107 | + require.NoError(t, err) |
| 108 | + } |
| 109 | + |
| 110 | + _, cacheExe, err := c.get(testID, executions) |
| 111 | + require.NoError(t, err) |
| 112 | + |
| 113 | + // cached emulator missed two executions |
| 114 | + assert.Len(t, cacheExe, 2) |
| 115 | + assert.Equal(t, 3, cacheExe[0].Index) |
| 116 | + assert.Equal(t, 4, cacheExe[1].Index) |
| 117 | + }) |
| 118 | +} |
0 commit comments